json的循环,把后台传过来的json格式转化成自己想要的数据格式

实例:后台传过来的json数据如图所示:

而我们想要的数据格式为[{time: xx1, value: xx1}, {time: xx2, value: xx2}, {time: xx3, value: xx3}]

废话不多说,上代码实现:

1             let json = {"a": 1, "b": 2, "c": 3};
2             let arr = [];
3             
4             for(let key in json) {
5                 arr.push( {time: key, value: json[key]} )
6             }
7 
8             console.log(arr);
9             // [{time: "a", value: 1}, {time: "b", value: 1}, {time: "c", value: 1}]

若只想得到["a", "b", "c"] 或者[1, 2, 3]的数组数据, 只需要向数组里push进  key 或者 json[key]即可哦

 

posted @ 2018-03-06 21:03  Various  阅读(612)  评论(0编辑  收藏  举报