Reputation: 8589
I don't want to use any for loop or any of the regular loops, I am trying to use a forEach
but I am getting an error
Uncaught TypeError: data.forEach is not a function
return falsyData.map(function(data) {
data.forEach(function(key) {
if (key.match(reg)) {
return key;
}
});
});
but if I do it this way it works:
return falsyData.map(function(data) {
for (var key in data) {
if (key.match(reg)) {
return key;
}
}
});
why ?
Upvotes: 5
Views: 14743
Reputation: 104775
data
is an object - forEach
only runs on Array types. You have to use a for..in
loop to iterate the keys of an object.
Upvotes: 8