我的目标是创建一个函数,该函数返回一个新的对象,该对象由作为名称和值的键组成,作为玩家编号:
var newObj = {"Brandon Bass": 2, "Tarik Black":19,...}
我有这个对象:
var playerObj = {
players: [{
name: "Brandon Bass",
number: 2,
stats: {
points: 2.5
}
},
{
name: "Tarik Black",
number: 19,
stats: {
points: 2.5
}
}, {
name: "Anothy Broqn",
number: 11,
stats: {
points: 4.8
}
},
{
name: "Kobe Bryant",
number: 22,
stats: {
points: 5.6
}
}
]};
这是my most recent attempt。这可能吗?
for (var key in playerObj.players) {
var newObj = {};
for (var i = 0; i < playerOjb.players.length; i++) {
newObj[playerObj.players[i]] = playerObj.players[key].number;
}
return newObj;
}