将 Key、Value 对添加到现有数组,条件是格式化 Key

Add Key, Value pair to existing array with a condition to format the Key

我有以下数组:

  var array = [
    [
      '20160221',
      '10'
    ],
    [
      '20160307',
      '20'
    ]
  ];

想转换为以下格式(同时将日期从 20160221 更改为 2016-02-21)

var new = [ {date: '2016-02-21', value: '10'}, {date: '2016-03-07', value: '20'} ];

谢谢。

这是一个示例代码:

var array = [
    [
      "20160221",
      "10"
    ],
    [
      "20160307",
      "20"
    ]
  ];

var newArr =  array.map(function(item){
  return {date:item[0].replace(/(\d{4})(\d{2})(\d{2})/,'--'),value:item[1]};
});

console.log(newArr);  

demo