首页 > 解决方案 > jQuery loop over JSON result

问题描述

I am trying to reformat the result of string json On the jQuery AJAX success callback I want to loop over the results of the object and change date type or format from string to date and value also like This is an example ,

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"}]');

I want to format to be like this format

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
},];

标签: javascriptjqueryjsonajax

解决方案


您可以使用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)


推荐阅读