就当在这里对自己的成长过程做个记录吧,虽然仍然很菜,要学习的仍然很多,但是我相信日积月累,行胜于言。
下面先把用到的json数据介绍一下。
resultJson 数据是需要重组的数据,里面没有站点的经纬度和站名信息,需要我将站名和经纬度添加到resultJson数据中。
var resultJson = [{
"actVal": 1022,
"datetime": 1585736100000,
"descript": "",
"entryTime": 1585736880036,
"id": "f3dd1f82-7559-429e-8e32-16c9cdb85841",
"isHour": 0,
"stationId": "499",
"stationType": "地方站",
"thresholdType": "气压",
"thresholdVal": 1013,
"userId": "000001"
}, {
"actVal": 1023.1,
"datetime": 1585736100000,
"descript": "",
"entryTime": 1585736880067,
"id": "01ef3754-598c-4a75-a103-39fadd38037e",
"isHour": 0,
"stationId": "407",
"stationType": "地方站",
"thresholdType": "气压",
"thresholdVal": 1013,
"userId": "000001"
}];
下面是获取到的站名和经纬度的json数据:
var stationInfo = {
"499": {
"station_id": "499",
"station_name": "可可",
"lat": "31.8",
"lon": "104.6"
},
"407": {
"station_id": "407",
"station_name": "哈哈",
"lat": "31.85",
"lon": "104.6"
},
"498": {
"station_id": "498",
"station_name": "啦啦",
"lat": "31.85",
"lon": "104.6"
}
}
用jQuery 的foreach循环来重组数据,特别方便,关于foreach循环不明白的小白,可以查看我的上一篇文章,有简单介绍。
// 数据整理
$.each(resultJson, function (i, feature) {
feature.stationName = stationInfo[feature.stationId].station_name;//向resultJson插入站名
feature.lon = stationInfo[feature.stationId].lon;//向resultJson插入经度
feature.lat = stationInfo[feature.stationId].lat;//向resultJson插入纬度
feature.datetime = timestampToTime(feature.datetime);//timestampToTime是将时间戳转换为YYYY-mm-dd的函数
});
我记录的这种处理方式得需要stationInfo中的数据都是这种键值对的形式,通过stationInfo[feature.stationId]获取对应的stationInfo中的站点,然后再通过stationInfo[feature.stationId].lon,stationInfo[feature.stationId].lat,stationInfo[feature.stationId].station_name来得到经纬度和站名信息。
这样返回后的resultJson数据就变成了
重组后的resultJson如下
resultJson = [{ "actVal": 1022, "datetime": 2020-01-01, "descript": "", "entryTime": 1585736880036, "id": "f3dd1f82-7559-429e-8e32-16c9cdb85841", "isHour": 0, "stationId": "499", "stationType": "地方站", "thresholdType": "气压", "thresholdVal": 1013, "userId": "000001" , "stationName": "可可", "lat": "31.8", "lon": "104.6"},
{ "actVal": 1023.1, "datetime": 2020-01-02, "descript": "", "entryTime": 1585736880067, "id": "01ef3754-598c-4a75-a103-39fadd38037e", "isHour": 0, "stationId": "407", "stationType": "地方站", "thresholdType": "气压", "thresholdVal": 1013, "userId": "000001" , "stationName": "哈哈", "lat": "31.85", "lon": "104.6"}];
如果这篇文章能帮到你,那真是太好了,继续加油吧,欢迎关注我哦~
听说打赏我的人,都进了福布斯排行榜,哈哈
打赏2块钱,帮我买杯咖啡,继续创作,谢谢大家!