我的网络服务结果是:
[
{
"spId":"SmTest_W1_Rf",
"type":"sp",
"latitude":32.816711,
"longitude":-117.25,
"spVolume":403.58157286047935,
"isInFlow":false,
"address":null,
"events":[
{
"eventName":"Leak",
"startDate":20140215,
"endDate":20140221,
"noOfDays":7,
"eventVolume":86.62499237060547
},
{
"eventName":"Leak",
"startDate":20140223,
"endDate":20140301,
"noOfDays":7,
"eventVolume":44.916664123535156
},
{
"eventName":"ReverseFlow",
"startDate":20140204,
"endDate":20140204,
"noOfDays":1,
"eventVolume":-1.6041665077209473
},
{
"eventName":"UsageInactive",
"startDate":20140204,
"endDate":20140205,
"noOfDays":2,
"eventVolume":3.609374761581421
},
{
"eventName":"UsageInactive",
"startDate":20140208,
"endDate":20140306,
"noOfDays":27,
"eventVolume":533.6527709960938
}
]
},
{
"spId":"SmTest_NPW1_Rf",
"type":"sp",
"latitude":32.816711,
"longitude":-117.25,
"spVolume":8983.332550048828,
"isInFlow":false,
"address":null,
"events":[
{
"eventName":"Leak",
"startDate":20121231,
"endDate":20141027,
"noOfDays":666,
"eventVolume":213674.984375
},
{
"eventName":"UsageInactive",
"startDate":20121231,
"endDate":20141027,
"noOfDays":666,
"eventVolume":213340.78125
}
]
}
]
预期结果是这样的:(不一样但格式相同)
[
{
"servicePoint":"67410074",
"isInFlow":0,
"eventType":"Reverse Flow",
"startDay":"201111010100",
"endDay":"201501050100",
"volume":10,
"noOfdays":5
},
{
"servicePoint":"67410074",
"isInFlow":0,
"eventType":"Metered Leaks",
"startDay":"201211010100",
"endDay":"201401050100",
"volume":9,
"noOfdays":3
},
{
"servicePoint":"67410074",
"isInFlow":0,
"eventType":"Tamper",
"startDay":"201311010100",
"endDay":"201401050100",
"volume":12,
"noOfdays":8
}
]
我的代码:
var popUpDetailsData = [];
var eventGridDetailsData = [];
for (var i = 0; i < gridData.length; i++) {
if (gridData[i].events.length !== 0) {
eventGridDetailsData.servicePoint = gridData[i].spId;
eventGridDetailsData.isInFlow = gridData[i].isInFlow;
for (var j = 0; j < gridData[i].events.length; j++) {
eventGridDetailsData.eventType = gridData[i].events[j].eventName;
eventGridDetailsData.startDay = gridData[i].events[j].startDate;
eventGridDetailsData.endDay = gridData[i].events[j].endDate;
eventGridDetailsData.volume = gridData[i].events[j].eventVolume;
eventGridDetailsData.noOfdays = gridData[i].events[j].noOfDays;
popUpDetailsData.push(eventGridDetailsData);
}
}
}
var popUpDetailsData = popUpDetailsData;
我正在重复数组值..我的意思是相同的数组值。 请帮帮我......我做错了..
答案 0 :(得分:1)
问题在于变量初始化的位置。
I've fiddled around with it来到这段代码:
var popUpDetailsData = [];
for (var i = 0; i < gridData.length; i++) {
if (gridData[i].events.length !== 0) {
for (var j = 0; j < gridData[i].events.length; j++) {
var eventGridDetailsData = [];
eventGridDetailsData.servicePoint = gridData[i].spId;
eventGridDetailsData.isInFlow = gridData[i].isInFlow;
eventGridDetailsData.eventType = gridData[i].events[j].eventName;
eventGridDetailsData.startDay = gridData[i].events[j].startDate;
eventGridDetailsData.endDay = gridData[i].events[j].endDate;
eventGridDetailsData.volume = gridData[i].events[j].eventVolume;
eventGridDetailsData.noOfdays = gridData[i].events[j].noOfDays;
popUpDetailsData.push(eventGridDetailsData);
}
}
}
如您所见,我已将var eventGridDetailsData
置于第二个for循环中。这是因为我们每次都想要一个CLEAR数组对象(没有前一次的剩余时间)。
servicePoint and isInFlow
相同,但这是因为变量稍后放置,所以这些行也必须稍后放置......
答案 1 :(得分:1)
使用ECMA5方法的示例。
var gridData = [
{
"spId":"SmTest_W1_Rf",
"type":"sp",
"latitude":32.816711,
"longitude":-117.25,
"spVolume":403.58157286047935,
"isInFlow":false,
"address":null,
"events":[
{
"eventName":"Leak",
"startDate":20140215,
"endDate":20140221,
"noOfDays":7,
"eventVolume":86.62499237060547
},
{
"eventName":"Leak",
"startDate":20140223,
"endDate":20140301,
"noOfDays":7,
"eventVolume":44.916664123535156
},
{
"eventName":"ReverseFlow",
"startDate":20140204,
"endDate":20140204,
"noOfDays":1,
"eventVolume":-1.6041665077209473
},
{
"eventName":"UsageInactive",
"startDate":20140204,
"endDate":20140205,
"noOfDays":2,
"eventVolume":3.609374761581421
},
{
"eventName":"UsageInactive",
"startDate":20140208,
"endDate":20140306,
"noOfDays":27,
"eventVolume":533.6527709960938
}
]
},
{
"spId":"SmTest_NPW1_Rf",
"type":"sp",
"latitude":32.816711,
"longitude":-117.25,
"spVolume":8983.332550048828,
"isInFlow":false,
"address":null,
"events":[
{
"eventName":"Leak",
"startDate":20121231,
"endDate":20141027,
"noOfDays":666,
"eventVolume":213674.984375
},
{
"eventName":"UsageInactive",
"startDate":20121231,
"endDate":20141027,
"noOfDays":666,
"eventVolume":213340.78125
}
]
}
];
var popUpDetailsData = gridData.reduce(function (accumulate, item) {
item.events.forEach(function (event) {
accumulate.push({
servicePoint: this.spId,
isInFlow: this.isInFlow,
eventType: event.eventName,
startDay: event.startDate,
endDay: event.endDate,
volume: event.eventVolume,
noOfdays: event.noOfDays
});
}, item);
return accumulate;
}, []);
document.getElementById('out').textContent = JSON.stringify(popUpDetailsData, null, 2);
&#13;
<pre id="out"></pre>
&#13;