2016-01-21 62 views
61

我有了做类似如何在javascript中迭代(键,值)?

for((key,value) in dictionary){ 
    //Do stuff where key would be 0 and value would be the object 
} 
+1

'的(让Object.entries(OBJ)的[键,值])',需要巴贝尔。 – elclanrs

+3

[可能重复](http://stackoverflow.com/questions/7241878/for-in-loops-in-javascript-key-value-pairs) – Tholle

+0

@elclanrs它在ES2016,它还没有标准化:-) – thefourtheye

回答

100

的TL的

dictionary = {0: {object}, 1:{object}, 2:{object}} 

格式怎样才能通过这本词典迭代的字典;博士

  1. 在ECMAScript中5 , 这不可能。
  2. 在ECMAScript 2015中,有可能使用Map s。
  3. 在ECMAScript 2017中,它将随时可用。

的ECMAScript 5:

不,它不是与物体可能。

你应利用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]); 
}); 

的ECMAScript 2015年

在ECMAScript中2015年,您可以使用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 

的ECMAScript 2017年

的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 
+2

ECMA 2015中的地图功能非常棒。感谢您保持信息的最新状态。 +1。 – RBT

4

试试这个:

var value; 
for (var key in dictionary) { 
    value = dictionary[key]; 
    // your code here... 
} 
13

试试这个:

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"} 
0

你可以做这样的事情:

dictionary = {'ab': {object}, 'cd':{object}, 'ef':{object}} 
var keys = Object.keys(dictionary); 

for(var i = 0; i < keys.length;i++){ 
    //keys[i] for key 
    //dictionary[keys[i]] for the value 
} 
12

由于ES2017(所以要小心browser support)在问题的first comment已标准化提到Object.entries()方法:

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我们可以遍历创建阵列

  • 由于我们保证的每个项目,每个这样迭代数组中元素是另一种双项数组,我们可以使用destructuring来直接将变量keyvalue分配给其第一项和第二项。

1

使用招摇,ui.js

你可以做到这一点 -

_.forEach({ 'a': 1, 'b': 2 }, function(n, key) { 
    console.log(n, key); 
});