javascript - Js lodash return the same sample every time -


i want generate samples lodash , return me same numbers each line. im doing wrong?

var primarynumscells = _.range(50); var extranumscells = _.range(20);  var lottery = {lineconfigurations: []};   var numsconfig = {linenumbers: {}};      for( var = 0; < 2; ++ ) {        numsconfig.linenumbers.primarynumbers = _.sample(primarynumscells, 5);       numsconfig.linenumbers.secondarynumbers = _.sample(extranumscells, 2);       lottery.lineconfigurations.push(numsconfig);     }  console.log(lottery); 

the results of first object , second object of primary , secondary numbers same;

here fiddle: http://jsbin.com/vavuhunupi/1/edit

create new object inside loop. it's easy plain object literal (dropping variable):

var lottery = {lineconfigurations: []}; (var = 0; < 2; i++) {   lottery.lineconfigurations.push({     linenumbers: {       primarynumbers: _.sample(primarynumscells, 5),       secondarynumbers: _.sample(extranumscells, 2)     }   }); } 

as stands, @ each step of loop modify , push the same object (stored in numsconfig var).


and here goes lodash way of doing same thing:

var lottery = {   lineconfigurations: _.map(_.range(2), function() {     return {       linenumbers: {         primarynumbers: _.sample(primarynumscells, 5),         secondarynumbers: _.sample(extranumscells, 2)       }     };   }) }; 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -