如何在数组中添加元素作为JavaScript中对象的属性

时间:2014-11-04 19:26:15

标签: javascript arrays object

假设我有一个数组:

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

一个对象:

var band = {};

有没有办法迭代数组,以便成员数组中的每个元素都将作为带对象的属性添加,并将值作为对象?

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

1 个答案:

答案 0 :(得分:2)

尝试:

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

members.forEach(function(member) {
    band[member] = {};
});