javascript中getOwnPropertyNames的增量索引

时间:2015-06-09 18:00:08

标签: javascript arrays sorting

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

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

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

console.log(pickedCards);

1 个答案:

答案 0 :(得分:3)

尝试:



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

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

document.write(JSON.stringify(pickedCardsChanges))