Why is “forEach not a function” for this object?

bject 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”]

Leave a Comment