Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  141] [ 3]  / answers: 1 / hits: 186256  / 9 Years ago, sun, june 28, 2015, 12:00:00

This is probably something really dumb, but I don't understand why this doesn't work.



var a = {cat:large};

a.forEach(function(value, key, map){
console.log(value);
});


Uncaught TypeError: a.forEach is not a function



http://jsfiddle.net/ty7z6pse/


More From » object

 Answers
139

Object does not have forEach, it belongs to Array prototype. If you want to iterate through each key-value pair in the object and take the values. You can do this:



Object.keys(a).forEach(function (key){
console.log(a[key]);
});


Usage note: For an object v = {cat:large, dog: small, bird: tiny};, Object.keys(v) gives you an array of the keys so you get [cat,dog,bird]


[#66011] Friday, June 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikhilc

Total Points: 128
Total Questions: 100
Total Answers: 89

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;