I have one variable with json object.
var seriesData = [{
feederId: "TUR 112",
businessEventCode: "LowVoltage",
servicePointEventCount: 4
}, {
feederId: "TUR 112",
businessEventCode: "HighVoltage",
servicePointEventCount: 2
}, {
feederId: "ABC",
businessEventCode: "LowVoltage",
servicePointEventCount: 4
}, {
feederId: "ABC",
businessEventCode: "HighVoltage",
servicePointEventCount: 1
}];
I want to Mock this data into this json formet
var MockService= [{
feederId: "TUR 112",
servicePointEventLowCount: 4,
servicePointEventHighCount: 2
}, {
feederId: "ABC",
servicePointEventLowCount: 6,
servicePointEventHighCount:3
}];
For that My code :
if (seriesData) {
var mockdata= [];
for (var i = 0; i < seriesData.length; i++) {
var data = {};
data.feederId = seriesData[i].feederId;
if (seriesData[i].businessEventCode == 'LowVoltage') {
data.servicePointEventLowCount = seriesData[i].servicePointEventCount;
} else {
data.servicePointEventHighCount = seriesData[i].servicePointEventCount;
}
mockdata.push(data);
}
}
var seriesDataMock = mockdata;
I am getting response as -
[{
feederId: "TUR 112",
servicePointEventLowCount: 4,
}, {
feederId: "TUR 112",
servicePointEventHighCount: 2
}, {
feederId: "ABC",
servicePointEventLowCount: 6,
}, {
feederId: "ABC",
servicePointEventLowCount: 6,
}];
But I want it to be like this--
[{
feederId: "TUR 112",
servicePointEventLowCount: 4,
servicePointEventHighCount: 2
}, {
feederId: "ABC",
servicePointEventLowCount: 6,
servicePointEventHighCount:3
}];
Combined values for feederID...
Please guide me if I m missing something.
Thanks!
You set your Mockdata to an empty array, then you proceed to overwrite the same properties for each iteration. I think you want to append data objects for each iteration like this:
pseudo:
for (var i = 0; i < seriesData.length; i++) {
var data = {};
data.feederId = seriesData[i].feederId;
Mockdata.push(data);
...
}
EDIT: I read your updated question and it seems like you want to merge each two sets into one? An easy way would be to do this instead:
for (var i = 0; i < seriesData.length; i+=2) {
var data = {};
data.feederId = seriesData[i].feederId;
data.servicePointEventLowCount = seriesData[i].servicePointEventCount;
data.servicePointEventHighCount = seriesData[i+1].servicePointEventCount;
Mockdata.push(data);
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.