javascript - Lodash forEach Associative Array -
javascript - Lodash forEach Associative Array -
is there foreach
loop in lodash
associative arrays? function called "foreach", i've found, works indexed arrays. example, if have array myarray
values [1, 2, 3]
, , do
lodash.foreach(myarray, function(index) { console.log(index); });
and run function (in node
), expected result:
1 2 3
however, when seek associative array, doesn't work:
lodash = require('lodash'); myarray = []; myarray['valone'] = 1; myarray['valtwo'] = 2; myarray['valthree'] = 3; lodash.foreach(myarray, function(index) { console.log('7'); });
as can see running in node
, callback function doesn't fire when includes other array elements. seems skip loop entirely.
first of all, why happen? sec of all, there function included in lodash
problem, or, if not, there way utilize foreach
function accomplish this, without changing original array in process?
lodash
has function forown
purpose. in sec array, if do
_.forown(myarray, function(index) { console.log(index); });
you should intended result.
i'm still not sure why foreach
seems skip first function, however, believe may have array not having "length". javascript
array's length highest numbered index has. example, array myotherarray
defined myotherarray[999]="myvalue"
have length of 1,000 (because arrays zero-indexed, meaning start @ 0, not 1), if has no other values. means array no numbered indexes, or negative indexes, not have length
attribute. lodash
must picking on , not giving array length
attribute, maintain consistency , performance, not rendering output.
javascript arrays node.js foreach lodash
Comments
Post a Comment