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
4 个解决方案
#1
36
It's not a map. It's simply an Object
.
这不是地图。它只是一个对象。
Edit: below code is worse than OP's, as Amit pointed out in comments.
编辑:正如阿米特在评论中所指出的,下面的代码比OP更差。
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);
});
#2
6
In the sense I think you intended, in ES5 or ES2015, no, not without some work on your part.
从某种意义上说,我认为你打算在ES5或ES2015中,不,不是没有你的工作。
In ES2016, probably with object.values
.
在ES2016中,可能使用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.
请注意,JavaScript中的数组实际上是从整数到值的映射,JavaScript数组中的值可以直接枚举。
['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
:
此外,在ES2015中,您可以通过将函数放在名为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).
另外,根据其他答案,还有其他内置函数可以提供您想要的功能,例如Map(但不是WeakMap,因为它不可迭代)和Set例如(但这些并不存在于所有浏览器中)。
#3
#4
0
You could use underscore.js and the each function:
你可以使用underscore.js和每个函数:
_.each({key1: "value1", key2: "value2"}, function(value) {
console.log(value);
});
#1
36
It's not a map. It's simply an Object
.
这不是地图。它只是一个对象。
Edit: below code is worse than OP's, as Amit pointed out in comments.
编辑:正如阿米特在评论中所指出的,下面的代码比OP更差。
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);
});
#2
6
In the sense I think you intended, in ES5 or ES2015, no, not without some work on your part.
从某种意义上说,我认为你打算在ES5或ES2015中,不,不是没有你的工作。
In ES2016, probably with object.values
.
在ES2016中,可能使用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.
请注意,JavaScript中的数组实际上是从整数到值的映射,JavaScript数组中的值可以直接枚举。
['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
:
此外,在ES2015中,您可以通过将函数放在名为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).
另外,根据其他答案,还有其他内置函数可以提供您想要的功能,例如Map(但不是WeakMap,因为它不可迭代)和Set例如(但这些并不存在于所有浏览器中)。
#3
1
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
Map类型确实有一个values()方法,它返回值的迭代器
#4
0
You could use underscore.js and the each function:
你可以使用underscore.js和每个函数:
_.each({key1: "value1", key2: "value2"}, function(value) {
console.log(value);
});