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
Post a Comment