Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  39] [ 2]  / answers: 1 / hits: 132764  / 9 Years ago, thu, november 26, 2015, 12:00:00

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

More From » dictionary

 Answers
9

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

[#64259] Tuesday, November 24, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janiajohnnad

Total Points: 146
Total Questions: 92
Total Answers: 107

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
janiajohnnad questions
Tue, Mar 30, 21, 00:00, 3 Years ago
Sun, Feb 7, 21, 00:00, 3 Years ago
Fri, Nov 6, 20, 00:00, 4 Years ago
Thu, Jun 18, 20, 00:00, 4 Years ago
;