2016-01-21 63 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); 
});