我有从Web服务返回的json对象。 Web服务对象:
var seriesData = [{
feederId: "PTS113T",
businessEventCode: "LowVoltage",
servicePointEventCount: 6,
startDayID: 20130812,
}, {
feederId: "PTS113T",
businessEventCode: "HighVoltage",
servicePointEventCount: 2,
startDayID: 20130812,
}, {
feederId: "PTS113T",
businessEventCode: "LowVoltage",
servicePointEventCount: 7,
startDayID: 20130814,
}, {
feederId: "PTS113T",
businessEventCode: "HighVoltage",
servicePointEventCount: 5,
startDayID: 20130815,
}];
我想将该对象修改为..
我startDayID
startDayID
businessEventCode: "LowVoltage"
businessEventCode: "HighVoltage",
或var seriesData = [{
feederId: "PTS113T",
servicePointEventLowCount: 6,
servicePointEventHighCount: 2,
startDayID: 20130812,
}, {
feederId: "PTS113T",
servicePointEventLowCount: 7,
startDayID: 20130814,
}, {
feederId: "PTS113T",
servicePointEventHighCount: 5,
startDayID: 20130815,
}];
想要:
if (seriesData) {
var mockdata = [];
for (var i = 0; i < seriesData.length; i += 2) {
var data = {};
//data.feederId = seriesData[i].feederId;
if ((seriesData[i].startDayID) == (seriesData[i + 1].startDayID)) {
data.feederId = seriesData[i].feederId;
data.servicePointEventLowCount = seriesData[i].servicePointEventCount;
data.servicePointEventHighCount = seriesData[i + 1].servicePointEventCount;
data.startDayID = seriesData[i].startDayID;
} else {
data.feederId = seriesData[i].feederId;
data.startDayID = seriesData[i].startDayID;
if (seriesData[i].businessEventCode == 'LowVoltage') {
data.servicePointEventLowCount = seriesData[i].servicePointEventCount;
data.servicePointEventHighCount = 0;
} else {
data.servicePointEventHighCount = seriesData[i].servicePointEventCount;
data.servicePointEventLowCount = 0;
}
}
mockdata.push(data);
}
我在做什么:
{{1}}
答案 0 :(得分:1)
试试这个。
var mockdata = [];
// for check whether startDayID exist
//and temporary storage for data relative key(startDayID)
var mockDataKeyMap ={};
for (var i = 0; i < seriesData.length; i ++) {
var item = seriesData[i];
var key = item.startDayID;
var data = null;
if(!mockDataKeyMap[key]){
// startDayID do not exist create new
data = {};
// save for get after (below else sentence)
mockDataKeyMap[key] = data;
// push into
mockdata.push(data);
// set default info
data.feederId = item.feederId;
data.startDayID = item.startDayID;
}else{
// if startDayID exist get data from map
data= mockDataKeyMap[key];
}
if (item.businessEventCode === 'LowVoltage') {
data.servicePointEventLowCount = item.servicePointEventCount;
}
if (item.businessEventCode === 'HighVoltage') {
data.servicePointEventHighCount = item.servicePointEventCount;
}
}
使用"i+=2"
您的seriesData
始终没有数据LowVoltage
和HighVoltage
。
然后检查所有数据i++
然后仅检查密钥是否已存在
答案 1 :(得分:0)
尝试:
for (var i = 0; i < seriesData.length-1; i += 2) {
答案 2 :(得分:0)
根据您上次运行的循环,您在处理时不会seriesData[i + 1].startDayID
更好地检查此情况。