I received a JSON object from a web service with the following structure:
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,
}];
I need to transform this object into a new format where each startDayID
has separate counts for 'LowVoltage' and 'HighVoltage':
var seriesData = [{
feederId: "PTS113T",
servicePointEventLowCount: 6,
servicePointEventHighCount: 2,
startDayID: 20130812,
}, {
feederId: "PTS113T",
servicePointEventLowCount: 7,
startDayID: 20130814,
}, {
feederId: "PTS113T",
servicePointEventHighCount: 5,
startDayID: 20130815,
}];
To achieve this, I am looping through the original data and organizing it accordingly:
if (seriesData) {
var modifiedData = [];
for (var i = 0; i < seriesData.length; i += 2) {
var data = {};
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;
}
}
modifiedData.push(data);
}