55

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/

CertainPerformance
  • 313,535
  • 40
  • 245
  • 254
haventchecked
  • 1,809
  • 1
  • 20
  • 24
  • 6
    `forEach` is defined in `Array`'s `prototype`, not on `Object`. – thefourtheye Jun 28 '15 at 05:56
  • 1
    Because objects don't have `forEach`, arrays do – adeneo Jun 28 '15 at 05:56
  • 1
    do Maps not have this function? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach Is `var a = {};` not creating a Map? Why does an array get constructed with `[]` (since `forEach` works on a variable created like that) ? – haventchecked Jun 28 '15 at 06:00
  • 2
    @haventchecked No, a `Map` is a new ES6 standard. What you have is an `object literal`. If you want a `Map` instead, it's `var map = new Map(); map.set("cat", "large");` – CodingIntrigue Jun 28 '15 at 06:04

4 Answers4

108

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"]

thefourtheye
  • 221,210
  • 51
  • 432
  • 478
TaoPR
  • 5,722
  • 2
  • 23
  • 35
3

When I tried to access the result from

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

it was plain text result with no key-value pairs Here is an example

var fruits = {
    apple: "fruits/apple.png",
    banana: "fruits/banana.png",
    watermelon: "watermelon.jpg",
    grapes: "grapes.png",
    orange: "orange.jpg"
}

Now i want to get all links in a separated array , but with this code

    function linksOfPics(obJect){
Object.keys(obJect).forEach(function(x){
    console.log('\"'+obJect[x]+'\"');
});
}

the result of :

linksOfPics(fruits)



"fruits/apple.png"
 "fruits/banana.png"
 "watermelon.jpg"
 "grapes.png"
 "orange.jpg"
undefined

I figured out this one which solves what I'm looking for

  console.log(Object.values(fruits));
["fruits/apple.png", "fruits/banana.png", "watermelon.jpg", "grapes.png", "orange.jpg"]
Ahmed Younes
  • 804
  • 11
  • 15
0

If you really need to use a secure foreach interface to iterate an object and make it reusable and clean with a npm module, then use this, https://www.npmjs.com/package/foreach-object

Ex:

import each from 'foreach-object';
   
const object = {
   firstName: 'Arosha',
   lastName: 'Sum',
   country: 'Australia'
};
   
each(object, (value, key, object) => {
   console.log(key + ': ' + value);
});
   
// Console log output will be:
//      firstName: Arosha
//      lastName: Sum
//      country: Australia
Arosha
  • 873
  • 2
  • 9
  • 20
0

probably you are using string , consider that its a json obj

string_var.json() or JSON.parse(string_var) , now its usable in foreach

ulas korpe
  • 29
  • 1