0
我有一個數組,其中我需要總結每個鍵的所有值並獲得總數。然後我用每個鍵和總數創建一個新的數組。但是當我收到空數據時,我遇到了一個問題,並且我需要忽略數組對象內的{}。我遇到了一些問題這樣做的,解決這個,我試圖驗證,如果我的數據比{}帶{}的數組jave數據對象,我需要忽略它
var data = [{"data":{"since":1500896035232,"passerby":212,"live":159,"accesspoints":37,"associated":122,"knownvendor":139,"knownvendorinternal":109,"knownvendorcommon":107,"proberequest":75}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}},{"data":{}}];
var array=[];
var labels = [9];
var total = [9];
//iniciar a zero todos os elementos do array
for(var x=0; x<9;x++){
total[x] = 0;
}
var live=0, associated=0, ap=0, others=0;
var cas=0, official=0, common=0;
console.log("live data ",data[2]);
for(var i=0; i<data.length;i++){
if(data[i]!={}){
for(var j=1;j<9;j++){
switch(j){
case 1:
labels[j-1] = "passerby";
total[j-1] += data[i]['data'][labels[j-1]];
break;
case 2:
labels[j-1]="live";
total[j-1] += data[i]['data'][labels[j-1]];
live=total[j-1];
break;
case 3:
labels[j-1]="accesspoints";
total[j-1] += data[i]['data'][labels[j-1]];
ap=total[j-1];
break;
case 4:
labels[j-1]="associated";
total[j-1] += data[i]['data'][labels[j-1]];
associated=total[j-1];
break;
case 5:
labels[j-1]="knownvendor";
total[j-1] += data[i]['data'][labels[j-1]];
official=total[j-1];
break;
case 6:
labels[j-1]="knownvendorinternal";
total[j-1] += data[i]['data'][labels[j-1]];
cas=total[j-1];
break;
case 7:
labels[j-1]="knownvendorcommon";
total[j-1] += data[i]['data'][labels[j-1]];
common=total[j-1];
break;
case 8:
labels[j-1]="proberequest";
total[j-1] += data[i]['data'][labels[j-1]];
break;
}
}
}
}
for(var i=0; i<labels.length; i++){
var obj = {
"label": labels[i],
"value": total[i]
};
array.push(obj);
}
others1 = live - ap - associated;
var othersObj = {
"label": "others",
"value": others1
};
array.push(othersObj);
console.log("array ---> ", array);
console.log("data size ---> ", data.length);
console.log("-------------------------------");
console.log("live ---> ", live);
console.log("ap ---> ", ap);
console.log("associated ---> ", associated);
console.log("others ---> ", others1);
console.log("-------------------------------");
others2 = live-cas-official;
console.log("cas --> ",cas);
console.log("official --> ",official);
console.log("common --> ",common);
console.log("others ---> ", others2);
日誌
live data { data: {} }
array ---> [ { label: 'passerby', value: NaN },
{ label: 'live', value: NaN },
{ label: 'accesspoints', value: NaN },
{ label: 'associated', value: NaN },
{ label: 'knownvendor', value: NaN },
{ label: 'knownvendorinternal', value: NaN },
{ label: 'knownvendorcommon', value: NaN },
{ label: 'proberequest', value: NaN },
{ label: 'others', value: NaN } ]
data size ---> 20
-------------------------------
live ---> NaN
ap ---> NaN
associated ---> NaN
others ---> NaN
-------------------------------
cas --> NaN
official --> NaN
common --> NaN
others ---> NaN
感謝喲USO多,它的工作! –