461

I have a dictionary that has the format of

    dictionary = {0: {object}, 1:{object}, 2:{object}}

How can I iterate through this dictionary by doing something like

    for((key,value) in dictionary){
      //Do stuff where key would be 0 and value would be the object
    }
Zach
  • 467
  • 1
  • 2
  • 18
nbroeking
  • 6,010
  • 3
  • 15
  • 38
  • 12
    `for (let [key, value] of Object.entries(obj))`, need Babel. – elclanrs Jan 21 '16 at 01:14
  • 3
    [possible duplicate](http://stackoverflow.com/questions/7241878/for-in-loops-in-javascript-key-value-pairs) – Tholle Jan 21 '16 at 01:14
  • 1
    @elclanrs Its in ES2016 and it is not standardized yet :-) – thefourtheye Jan 21 '16 at 01:16
  • 2
    Possible duplicate of [For-each over an array in JavaScript?](http://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – sdc Jan 21 '16 at 01:20
  • @dooagain, it is not an array in this question. – zangw Jan 21 '16 at 01:23
  • Possible duplicate of [How to loop through key/value object in Javascript?](https://stackoverflow.com/questions/2958841/how-to-loop-through-key-value-object-in-javascript) – iammilind Sep 17 '18 at 07:02

11 Answers11

649

tl;dr

  1. In ECMAScript 2017, just call Object.entries(yourObj).
  2. In ECMAScript 2015, it is possible with Maps.
  3. In ECMAScript 5, it is not possible.

ECMAScript 2017

ECMAScript 2017 introduced a new Object.entries function. You can use this to iterate the object as you wanted.

'use strict';

const object = {'a': 1, 'b': 2, 'c' : 3};

for (const [key, value] of Object.entries(object)) {
  console.log(key, value);
}

Output

a 1
b 2
c 3

ECMAScript 2015

In ECMAScript 2015, there is not Object.entries but you can use Map objects instead and iterate over them with Map.prototype.entries. Quoting the example from that page,

var myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");

var mapIter = myMap.entries();

console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]

Or iterate with for..of, like this

'use strict';

var myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");

for (const entry of myMap.entries()) {
  console.log(entry);
}

Output

[ '0', 'foo' ]
[ 1, 'bar' ]
[ {}, 'baz' ]

Or

for (const [key, value] of myMap.entries()) {
  console.log(key, value);
}

Output

0 foo
1 bar
{} baz

ECMAScript 5:

No, it's not possible with objects.

You should either iterate with for..in, or Object.keys, like this

for (var key in dictionary) {
    // check if the property/key is defined in the object itself, not in parent
    if (dictionary.hasOwnProperty(key)) {           
        console.log(key, dictionary[key]);
    }
}

Note: The if condition above is necessary only if you want to iterate over the properties which are the dictionary object's very own. Because for..in will iterate through all the inherited enumerable properties.

Or

Object.keys(dictionary).forEach(function(key) {
    console.log(key, dictionary[key]);
});
Daniel
  • 17,154
  • 10
  • 55
  • 72
thefourtheye
  • 206,604
  • 43
  • 412
  • 459
  • 2
    A basic doubt here. I landed here looking for how to do this in node.js, which is javascript on server side. How do I know which ES version applies in my case. Also, in case of regular javascript users, what is the proper way to support as I understand that ES version depends on the client's browser? – Sandeepan Nath Aug 28 '18 at 13:43
  • 2
    @SandeepanNath You can use websites like https://node.green/ to know if a particular ES feature is supported in your Node.js. As far as browsers are concerned, people generally target the version which is widely supported, in this case, ES5. Apart from this, transpilers (like [Babel](https://babeljs.io/)) help convert ES2015+ code to ES5. – thefourtheye Aug 29 '18 at 07:51
  • 1
    I like `Object.keys(dictionary).forEach(function(key) {…` very readable, and compatible. – ctrl-alt-delor Dec 28 '18 at 10:50
  • 12
    `Object.entries(object).forEach(([key, val]) => {...});` – Krimson Feb 09 '19 at 07:01
  • ECMAScript 2015 solution above threw ["TypeScript and Iterator: Type 'IterableIterator' is not an array type"](https://stackoverflow.com/questions/49218765/typescript-and-iterator-type-iterableiteratort-is-not-an-array-type) but plain ol [myMap().foreach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach) worked well. – ttugates Apr 10 '19 at 18:13
  • To avoid excessive indentation, it would be neater to write for (var key in dictionary) {if (!parameters.hasOwnProperty(key)) {continue;} console.log(key, dictionary[key]);} – kloddant Oct 10 '19 at 20:06
96

Try this:

dict = {0:{1:'a'}, 1:{2:'b'}, 2:{3:'c'}}
for (var key in dict){
  console.log( key, dict[key] );
}

0 Object { 1="a"}
1 Object { 2="b"}
2 Object { 3="c"}
alex10
  • 1,970
  • 2
  • 15
  • 28
64

The Object.entries() method has been specified in ES2017 (and is supported in all modern browsers):

for (const [ key, value ] of Object.entries(dictionary)) {
    // do something with `key` and `value`
}

Explanation:

  • Object.entries() takes an object like { a: 1, b: 2, c: 3 } and turns it into an array of key-value pairs: [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ].

  • With for ... of we can loop over the entries of the so created array.

  • Since we are guaranteed that each of the so iterated array items is itself a two-entry array, we can use destructuring to directly assign variables key and value to its first and second item.

Loilo
  • 9,930
  • 5
  • 32
  • 41
39

WELCOME TO 2020 *Drools in ES6*

Theres some pretty old answers in here - take advantage of destructuring. In my opinion this is without a doubt the nicest (very readable) way to iterate an object.

const myObject = {
    nick: 'cage',
    phil: 'murray',
};

Object.entries(myObject).forEach(([k,v]) => {
    console.log("The key: ",k)
    console.log("The value: ",v)
})

Edit:

As mentioned by Lazerbeak, map allows you to cycle an object and use the key and value to make an array.

const myObject = {
    nick: 'cage',
    phil: 'murray',
};

const myArray = Object.entries(myObject).map(([k, v]) => {
    return `The key '${k}' has a value of '${v}'`;
});

console.log(myArray);

Edit 2:

To explain what is happening in the line of code:

Object.entries(myObject).forEach(([k,v]) => {}

Object.entries() converts our object to an array of arrays:

[["nick", "cage"], ["phil", "Murray"]]

Then we use forEach on the outer array:

1st loop: ["nick", "cage"]
2nd loop: ["phil", "murray"]

Then we "destructure" the value (which we know will always be an array) with ([k,v]) so k becomes the first name and v becomes the last name.

Steve
  • 1,827
  • 12
  • 24
  • 2
    Just a note: if you replace `forEach` with `map` above, it's then possible to aggregate values. `map` will then return a list of said values, thus potentially simplifying the code in other ways. – Lazerbeak12345 Jun 08 '20 at 17:35
24

Try this:

var value;
for (var key in dictionary) {
    value = dictionary[key];
    // your code here...
}
AMACB
  • 1,232
  • 1
  • 16
  • 24
22

You can do something like this :

dictionary = {'ab': {object}, 'cd':{object}, 'ef':{object}}
var keys = Object.keys(dictionary);

for(var i = 0; i < keys.length;i++){
   //keys[i] for key
   //dictionary[keys[i]] for the value
}
Dhaval Chaudhary
  • 3,908
  • 2
  • 20
  • 35
  • 3
    Beautiful! I love how your answer works in ECMAscript 5 despite the accepted and most upvoted answer saying it's not possible. You deserve all a lot more upvotes. – lilHar Jan 31 '19 at 01:31
5

I think the fast and easy way is

Object.entries(event).forEach(k => {
    console.log("properties ... ", k[0], k[1]); });

just check the documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

Jonathan
  • 399
  • 4
  • 7
  • 2
    even better: Object.entries(obj).forEach(([key, value]) => { console.log(`${key} ${value}`); }); – Hani Nov 22 '18 at 01:50
2

using swagger-ui.js

you can do this -

_.forEach({ 'a': 1, 'b': 2 }, function(n, key) {
    console.log(n, key);
 });
deeshank
  • 3,451
  • 4
  • 22
  • 29
2

You can use below script.

var obj={1:"a",2:"b",c:"3"};
for (var x=Object.keys(obj),i=0;i<x.length,key=x[i],value=obj[key];i++){
    console.log(key,value);
}

outputs
1 a
2 b
c 3

  • #will output #c 3 #1 a #2 b – Michael Piper Aug 19 '19 at 08:44
  • 4
    Consider adding an explanation to your answer, code alone is less helpful. Also, you can edit your answer, comments are not meant to be an extension to the answer in the way you are using them – Viktor Aug 19 '19 at 09:10
0

As an improvement to the accepted answer, in order to reduce nesting, you could do this instead, provided that the key is not inherited:

for (var key in dictionary) {
    if (!dictionary.hasOwnProperty(key)) {
        continue;
    }
    console.log(key, dictionary[key]);
}

Edit: info about Object.hasOwnProperty here

kloddant
  • 840
  • 5
  • 15
0

You can use JavaScript forEach Loop:

myMap.forEach((value, key) => {
    console.log('value: ', value);
    console.log('key: ', key);
});
Abilogos
  • 3,074
  • 2
  • 11
  • 27