将Key,Value对添加到现有数组,并使用条件格式化Key

时间:2016-03-07 07:33:41

标签: javascript javascript-objects

我有一个以下数组:

<script type="text/javascript" src="your_js_file.js"></script>

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

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

谢谢。

1 个答案:

答案 0 :(得分:1)

这是一个示例代码:

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

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

console.log(newArr);  

demo