0

I have json object return from the web service. Web service Object :

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 want to modified that object into..

I have startDayID for every startDayID there will be two value or single value for businessEventCode: "LowVoltage", or businessEventCode: "HighVoltage",

Want to :

var seriesData = [{
    feederId: "PTS113T",
    servicePointEventLowCount: 6,
    servicePointEventHighCount: 2,
    startDayID: 20130812,
}, {
    feederId: "PTS113T",
    servicePointEventLowCount: 7,
    startDayID: 20130814,
}, {
    feederId: "PTS113T",
    servicePointEventHighCount: 5,
    startDayID: 20130815,
}];

What i am doing :

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);
    }
2
  • Looks alright. How does the mockdata array look after loop has executed? Commented Sep 26, 2014 at 9:27
  • @springbo It is not displaying second last value... json value can be in any format so i cant compare i and i+2 value.. I guess u understood Commented Sep 26, 2014 at 9:30

3 Answers 3

1

try this.

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;
    }
}

it is very dangerous to use "i+=2"

Your seriesData always do not have couple of data LowVoltage and HighVoltage.

Then check all data i++

Then check only key is already exist or not

Sign up to request clarification or add additional context in comments.

Comments

0

try:

for (var i = 0; i < seriesData.length-1; i += 2) {

Comments

0

as per your loop for the last run you will not have seriesData[i + 1].startDayID better check this condition while processing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.