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 }
You have to pass the object you want to iterate, and the JavaScript Object. keys() method will return an array comprising all keys or property names. Then, you can iterate through that array and fetch the value of each property utilizing an array looping method such as the JavaScript forEach() loop.
There are two methods to iterate over an object which are discussed below: Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object.
Since Javascript 1.7 there is an Iterator object, which allows this: var a={a:1,b:2,c:3}; var it=Iterator(a); function iterate(){ try { console. log(it. next()); setTimeout(iterate,1000); }catch (err if err instanceof StopIteration) { console.
Object.entries(yourObj)
.Map
s.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); }
a 1 b 2 c 3
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); }
[ '0', 'foo' ] [ 1, 'bar' ] [ {}, 'baz' ]
Or
for (const [key, value] of myMap.entries()) { console.log(key, value); }
0 foo 1 bar {} baz
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]); });
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"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With