javascript - 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
},];
解决方案
您可以使用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)
推荐阅读
- react-native - 无法下载 React Canvas 绘图
- python - 如何使用 Python 从已知私钥生成以太坊公钥
- wpf - 如何删除 WPF 功能区组上的鼠标悬停效果?
- python - 彩色日志,但不基于日志级别
- scala - scala - 如何从另一个类访问案例类方法?
- python - Python SSH 作为 sudoer 连接
- javascript - Javascript 过滤器在 IE 上不起作用
- python - 从列表中将值添加到数据库中,该列表的值对应于 python 中的另一个数据库
- neo4j - 如何使用 gremlin 控制台或服务器获取 neo4j 3.4.5 的插件?
- javascript - 为什么当我使用 AJAX 重新加载页面时数据消失了