Iterate over values of object

JavascriptDictionary

Javascript Problem Overview


I want to iterate over all values of a map. I know it's possible to iterate over all keys. But is it possible to iterate directly over the values?

 var map = { key1 : 'value1', key2 : 'value2' }
 for (var key in map) { ...} // iterates over keys

Javascript Solutions


Solution 1 - Javascript

It's not a map. It's simply an Object.

Edit: below code is worse than OP's, as Amit pointed out in comments.

You can "iterate over the values" by actually iterating over the keys with:

var value;
Object.keys(map).forEach(function(key) {
    value = map[key];
    console.log(value);
});

Solution 2 - Javascript

I iterate like this and it works for me.

for (let [k, v] of myMap) {
    console.log("Key: " + k);
    console.log("Value: " + v);
}

Hope this helps :)

Solution 3 - Javascript

In the sense I think you intended, in ES5 or ES2015, no, not without some work on your part.

In ES2016, probably with object.values.

Mind you Arrays in JavaScript are effectively a map from an integer to a value, and the values in JavaScript arrays can be enumerated directly.

['foo', 'bar'].forEach(v => console.log(v)); // foo bar

Also, in ES2015, you can make an object iterable by placing a function on a property with the name of Symbol.iterator:

var obj = { 
    foo: '1', 
    bar: '2',
    bam: '3',
    bat: '4',
};

obj[Symbol.iterator] = iter.bind(null, obj);

function* iter(o) {
    var keys = Object.keys(o);
    for (var i=0; i<keys.length; i++) {
        yield o[keys[i]];
    }
}

for(var v of obj) { console.log(v); } // '1', '2', '3', '4'

Also, per other answers, there are other built-ins that provide the functionality you want, like Map (but not WeakMap because it is not iterable) and Set for example (but these are not present in all browsers yet).

Solution 4 - Javascript

EcmaScript 2017 introduced Object.entries that allows you to iterate over values and keys. Documentation

var map = { key1 : 'value1', key2 : 'value2' }

for (let [key, value] of Object.entries(map)) {
    console.log(`${key}: ${value}`);
}

The result will be: > key1: value1
> key2: value2

Solution 5 - Javascript

No, there's no direct method to do that with objects.

The Map type does have a values() method that returns an iterator for the values

Solution 6 - Javascript

In case you want to deeply iterate into a complex (nested) object for each key & value, you can do so using Object.keys():

const iterate = (obj) => {
    Object.keys(obj).forEach(key => {

    console.log(`key: ${key}, value: ${obj[key]}`)

    if (typeof obj[key] === 'object') {
            iterate(obj[key])
        }
    })
}

Solution 7 - Javascript

Anyone that comes across this, I managed to solve this in njs ( nginx js ) by doing the following:

>> var map = { key1 : 'value1', key2 : 'value2' }
>> for ( var i in map ){ console.log(i, map[i]) }
key1 value1
key2 value2

Solution 8 - Javascript

You could use underscore.js and the each function:

_.each({key1: "value1", key2: "value2"}, function(value) {
  console.log(value);
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthias MView Question on Stackoverflow
Solution 1 - JavascriptAndré ChalellaView Answer on Stackoverflow
Solution 2 - JavascriptParag JadhavView Answer on Stackoverflow
Solution 3 - JavascriptBen AstonView Answer on Stackoverflow
Solution 4 - JavascriptdmigoView Answer on Stackoverflow
Solution 5 - JavascriptAmitView Answer on Stackoverflow
Solution 6 - JavascriptMenelaos KotsollarisView Answer on Stackoverflow
Solution 7 - JavascriptallThingsLinuxView Answer on Stackoverflow
Solution 8 - JavascriptdprView Answer on Stackoverflow