How to iterate (keys, values) in javascript?

I have a dictionary that has a format

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

How can I iterate over this dictionary by doing something like

 for((key,value) in dictionary){ //Do stuff where key would be 0 and value would be the object } 
+235
javascript object iteration
Jan 21 '16 at 1:11
source share
9 answers

TL; dr

  1. In ECMAScript 5, this is not possible.
  2. In ECMAScript 2015, this is possible with Map s.
  3. In ECMAScript 2017, it will be easily accessible.

ECMAScript 5:

No, this is not possible with objects.

You must either for..in 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 only necessary if you want to iterate over properties that are property of the dictionary object. Because for..in will for..in over all inherited enumerated properties.

Or

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



ECMAScript 2015

In ECMAScript 2015, you can use Map objects and repeat them using Map.prototype.entries . Quoting an example from this 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 for..of 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); } 

Exit

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

Or

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

Exit

 0 foo 1 bar {} baz 



ECMAScript 2017

ECMAScript 2017 will introduce the new Object.entries feature. You can use this to iterate over the object as you like.

 'use strict'; const object = {'a': 1, 'b': 2, 'c' : 3}; for (const [key, value] of Object.entries(object)) { console.log(key, value); } 

Exit

 a 1 b 2 c 3 
+344
Jan 21 '16 at 1:14
source share

Try the following:

 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"} 
+40
Jan 21 '16 at 1:18
source share

The Object.entries() method was 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 iterate over each record of the created array.

  • Since we are guaranteed that each of the elements of the iterated array is a different array with two records, we can use destructuring to directly assign the key and value variables to its first and second element.

+32
Aug 17 '17 at 9:25
source share

Try the following:

 var value; for (var key in dictionary) { value = dictionary[key]; // your code here... } 
+12
Jan 21 '16 at 1:14
source share

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 } 
+9
Aug 17 '17 at 8:48 on
source share

I think a quick and easy way

 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

+2
Sep 26 '18 at 11:38
source share

using swagger-ui.js

You can do it -

 _.forEach({ 'a': 1, 'b': 2 }, function(n, key) { console.log(n, key); }); 
+1
Nov 29 '17 at 23:42 on
source share

You can use the script below.

 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); } 

exits
1 a
2 b
from 3

+1
Aug 19 '19 at 8:43
source share

As an improvement to the accepted answer, to reduce nesting, you can do this instead:

 for (var key in dictionary) { if (!dictionary.hasOwnProperty(key)) { continue; } console.log(key, dictionary[key]); } 
0
Dec 21 '18 at 17:30
source share



All Articles