2014-11-04 94 views
-1

讓說我有一個數組:如何在一個數組在JavaScript中添加元素爲對象的屬性

var members = ["Peter", "Paul", "Mary"]; 

和對象:

var band = {}; 

有沒有一種方式,通過迭代數組,這樣成員數組中的每個元素都將作爲帶對象的屬性添加爲值,並將其作爲對象?

band = {"Peter": {}, "Paul": {}, "Mary": {} } 
+1

在循環:'帶[成員[N]] = {};'。 – Teemu 2014-11-04 19:29:01

回答

2

嘗試:

var members = ["Peter", "Paul", "Mary"]; 
var band = {}; 

members.forEach(function(member) { 
    band[member] = {}; 
}); 
相關問題