Is there a forEach
loop in Lodash
for associative arrays? The function called forEach, I've found, only works for indexed arrays. For example, if I have an array myArray
with values [1, 2, 3]
, and do
lodash.forEach(myArray, function(index) {
console.log(index);
});
and run the function (in Node
), I get the expected result:
1
2
3
However, when I try this with an associative array, it 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 you can see from running this in Node
, the callback function doesn't fire even when it includes something other than the array elements. It just seems to skip the loop entirely.
First of all, why does this happen? Second of all, is there another function included in Lodash
for this problem, or, if not, is there a way to use the forEach
function to accomplish this, without changing the original array in the process?