假设我有一个数组:
var members = ["Peter", "Paul", "Mary"];
一个对象:
var band = {};
有没有办法迭代数组,以便成员数组中的每个元素都将作为带对象的属性添加,并将值作为对象?
band = {"Peter": {}, "Paul": {}, "Mary": {} }
答案 0 :(得分:2)
尝试:
var members = ["Peter", "Paul", "Mary"];
var band = {};
members.forEach(function(member) {
band[member] = {};
});