我正在尝试重新格式化字符串json的结果。在jQuery AJAX成功回调中,我想遍历对象的结果,并将日期类型或格式从字符串更改为日期和值,就像这是一个示例,>
var chartData1 = JSON.parse('[{"date":"2018-09-16T00:00:00","value":"10:02"},{"date":"2018-09-17T00:00:00","value":"10:37"},{"date":"2018-09-18T00:00:00","value":"10:25"}]');
我要格式化为这种格式
var chartData1 = [{
date: new Date(2018, 9, 16, 0, 0, 0, 0),
value: 10.2
}, {
date: new Date(2018, 9, 17, 0, 0, 0, 0),
value: 10.37
}, {
date: new Date(2018, 9, 18, 0, 0, 0, 0),
value: 10.25
},];
答案 0 :(得分:0)
您可以使用Array.map()来修改数组中的对象,例如:
var chartData1 = JSON.parse('[{"date":"2018-09-16T00:00:00","value":"10:02"},{"date":"2018-09-17T00:00:00","value":"10:37"},{"date":"2018-09-18T00:00:00","value":"10:25"}]');
var result = chartData1.map(e => ({
date: new Date(e.date),
value: e.value.replace(':', '.')
}))
console.log(result)