Javascript 迭代对象的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33946567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over values of object
提问by Matthias M
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
回答by André Chalella
It's not a map. It's simply an Object
.
这不是地图。它只是一个Object
.
Edit:below code is worse than OP's, as Amit pointed out in comments.
编辑:正如 Amit 在评论中指出的那样,下面的代码比 OP 更糟糕。
You can "iterate over the values" by actually iterating over the keyswith:
您可以通过实际迭代键来“迭代值” :
var value;
Object.keys(map).forEach(function(key) {
value = map[key];
console.log(value);
});
回答by Parag Jadhav
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 :)
希望这可以帮助 :)
回答by Ben Aston
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 notWeakMap
because it is not iterable) and Set
for example (but these are not present in all browsers yet).
此外,根据其他答案,还有其他内置程序可以提供您想要的功能,例如Map
(但不是WeakMap
因为它不可迭代)和Set
例如(但这些尚未出现在所有浏览器中)。
回答by dmigo
EcmaScript 2017 introduced Object.entries
that allows you to iterate over values and keys. Documentation
引入的 EcmaScript 2017Object.entries
允许您迭代值和键。文档
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
键 1
:值1 键2:值 2
回答by Menelaos Kotsollaris
In case you want to deeply iterateinto a complex (nested) object for each key & value, you can do so using Object.keys():
如果您想为每个key & value深度迭代到一个复杂的(嵌套)对象,您可以使用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])
}
})
}
回答by Amit
回答by dpr
You could use underscore.js and the each function:
您可以使用underscore.js 和 each 函数:
_.each({key1: "value1", key2: "value2"}, function(value) {
console.log(value);
});