This article illustrates how JavaScript implements the construction of json arrays. Share for your reference, as follows:
The data from the back end is an array, and the data contained in each element is as follows (the same host record is adjacent)
currentTime:"1470729601"
host:"10.3.34.21"
taskList:null
taskNum:1
You want to construct a json array similar to the following structure
[{ name: 'host:10.1.6.49', data: [ [1470641461000, 5], [1470642581000, 4], [1470643701000, 2], [1470647341000, 3] ] }, { name: 'host:10.3.34.18', data: [ [1470641461000, 2], [1470642581000, 2], [1470643701000, 1], [1470647341000, 4] ] }, { name: 'host:10.2.2.22', data: [ [1470641461000, 3], [1470642581000, 2], [1470643701000, 6], [1470647341000, 4] ] },{ name: 'host:10.1.110.96', data: [ [1470641461000, 1], [1470642581000, 8], [1470643701000, 1], [1470647341000, 1] ] },{ name: 'host:10.2.2.87', data: [ [1470641461000, 7], [1470642581000, 3], [1470643701000, 6], [1470647341000, 5] ] }]
The wrong way:
var backendData = data.result; var resultList = new Array(); var curHost = ""; var oneHostDataList = new Array(); for (var i in backendData) { var host = backendData[i].host; if (host != curHost) { if(i != 0) { var item = new Object(); item.name = curHost; item.data = oneHostDataList; resultList.push(item); } oneHostDataList = new Array(); curHost = host; } else { var dot = new Array(); dot.push(backendData[i].currentTime * 1000); dot.push(backendData[i].taskNum); oneHostDataList.push(dot); } } var item = new Object(); item.name = curHost; item.data = oneHostDataList; resultList.push(item); $scope.data = JSON.stringify(resultList);
The problem with the above method is that the conversion will cause name and host with quotation marks. The correct way is as follows:
var backendData = data.result; var resultList = []; var curHost = ""; var oneHostDataList = new Array(); for (var i in backendData) { var host = backendData[i].host; if (host != curHost) { if(i != 0) { var item = { name: curHost, data: oneHostDataList }; resultList.push(item); } oneHostDataList = new Array(); curHost = host; } else { var dot = new Array(); dot.push(backendData[i].currentTime * 1000); dot.push(backendData[i].taskNum); oneHostDataList.push(dot); } } var item = new Object(); item.name = curHost; item.data = oneHostDataList; resultList.push(item); $scope.data = resultList;
Last
In order to help you make learning easy and efficient, we will share a large number of information for free, and help you overcome difficulties on the way to become a whole stack engineer and even an architect. Here we recommend a front-end full stack learning exchange circle: 866109386 Welcome to join the group for discussion, study and exchange, and make progress together.
When you really start learning, you will inevitably not know where to start, which leads to inefficiency and affects the confidence to continue learning.
But the most important thing is not to know which technologies need to be mastered, learning frequently trample pits, and ultimately waste a lot of time, so it is necessary to have effective resources.
Finally, I wish all the front-end programmers who encounter bottle diseases and don't know what to do. I wish you all the best in your future work and interviews.