我有一本字典,格式为
dictionary = {0: {object}, 1:{object}, 2:{object}}
我怎样才能通过做类似的事情来遍历这本字典
for ((key, value) in dictionary) {
    //Do stuff where key would be 0 and value would be the object
}
我有一本字典,格式为
dictionary = {0: {object}, 1:{object}, 2:{object}}
我怎样才能通过做类似的事情来遍历这本字典
for ((key, value) in dictionary) {
    //Do stuff where key would be 0 and value would be the object
}
Object.entries(yourObj).Maps。ECMAScript 2017 引入了一个新Object.entries函数。您可以根据需要使用它来迭代对象。
'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
在 ECMAScript 2015 中,没有,Object.entries但您可以使用Map对象代替并使用Map.prototype.entries. 引用该页面的示例,
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"]
或者for..of像这样迭代
'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' ]
或者
for (const [key, value] of myMap.entries()) {
  console.log(key, value);
}
0 foo
1 bar
{} baz
不,对象是不可能的。
您应该像这样使用for..in, 或进行迭代Object.keys
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]);
    }
}
注:在if上面的条件是必要的前提是你要遍历这是属性dictionary的对象是自己的。因为for..in会遍历所有继承的可枚举属性。
或者
Object.keys(dictionary).forEach(function(key) {
    console.log(key, dictionary[key]);
});
试试这个:
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"}
该Object.entries()方法已在 ES2017 中指定(并且在所有现代浏览器中均受支持):
for (const [ key, value ] of Object.entries(dictionary)) {
    // do something with `key` and `value`
}
解释:
Object.entries()采用类的对象{ a: 1, b: 2, c: 3 },并把它变成键-值对的数组:[ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]。
随着for ... of我们可以遍历所谓创建的数组的条目。
由于我们保证,每个迭代使阵列项目本身是一个双项阵列,我们可以用解构直接分配变量key,并value在其第一和第二项。
欢迎来到 2020 * ES6 中的 Drools*
这里有一些非常古老的答案 - 利用解构。在我看来,这无疑是迭代对象的最好(非常易读)的方式。
const myObject = {
    nick: 'cage',
    phil: 'murray',
};
Object.entries(myObject).forEach(([k,v]) => {
    console.log("The key: ", k)
    console.log("The value: ", v)
})
编辑:
正如 Lazerbeak 所提到的,map允许您循环一个对象并使用键和值来创建数组。
const myObject = {
    nick: 'cage',
    phil: 'murray',
};
const myArray = Object.entries(myObject).map(([k, v]) => {
    return `The key '${k}' has a value of '${v}'`;
});
console.log(myArray);
编辑2:
解释代码行中发生的事情:
Object.entries(myObject).forEach(([k,v]) => {}
Object.entries() 将我们的对象转换为数组数组:
[["nick", "cage"], ["phil", "murray"]]
然后我们在外部数组上使用 forEach:
1st loop: ["nick", "cage"]
2nd loop: ["phil", "murray"]
然后我们“解构”这个值(我们知道它总是一个数组),([k,v])sok成为名字并v成为姓氏。
试试这个:
var value;
for (var key in dictionary) {
    value = dictionary[key];
    // your code here...
}