weixin_33724059 2018-10-15 17:15 采纳率: 0%
浏览 37

jQuery遍历JSON结果

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
},];
  • 写回答

1条回答 默认 最新

  • elliott.david 2018-10-15 17:33
    关注

    you can use Array.map() to modify the objects in the array like :

    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)

    </div>
    
    评论

报告相同问题?