JS:重新插入数组

时间:2014-03-14 10:08:56

标签: javascript

所以我有这个数组:

[Object, Object]
  [0]: Object
    ['date']: "2013-12"
    ['views']: 21023
  [1]: Object
    ['date']: "2014-01"
    ['views']: 21312
    length: 2

我怎样才能成功

 [Object, Object]
   ['date']: Object
      [0]: "2013-12"
      [1]: "2014-01"
   ['views']: Object
      [0]: 21023
      [1]: 21312

显然这段代码不起作用:

            for (var i = 0; i < oldArray.length; i++) {
                newArray['date'][i] = oldArray[i]['date'];
                newArray['views'][i] = oldArray[i]['views'];
            };

提前致谢!

1 个答案:

答案 0 :(得分:1)

您期望的结果不是数组,应该只是一个对象。

var result = { date: [], views: [] };
for (var i = 0; i < oldArray.length; i++) {
    result['date'].push(oldArray[i]['date']);
    result['views'].push(oldArray[i]['views']);
};