使用这种类似数组的对象可以获取每个属性的索引,但是在记录该对象时无法输出结果。我在这里做错了什么?

// Array-like object
var pickedCards = { 0: 'a', 1: 'b', 2: 'c' };

Object.getOwnPropertyNames(pickedCards).forEach(function(val) {
  val++;
  return pickedCards[val];
});

console.log(pickedCards);

最佳答案

尝试:



var pickedCards = { 0: 'a', 1: 'b', 2: 'c' }, pickedCardsChanges = {};

for(var card in pickedCards){
  pickedCardsChanges[+card + 1] = pickedCards[card]
}

document.write(JSON.stringify(pickedCardsChanges))

10-08 00:20