0

I have the following json array

{"label":[{"name":"Government Schools",
          "datapoints":[{"year":"2007-2008","total":"1300"},
                        {"year":"2008-2009","total":"1280"},
                        {"year":"2009-2010","total":"1100"},
                        {"year":"2010-2011","total":"1400"}]
         },
         {"name":"Total Schools",
          "datapoints":[{"year":"2007-2008","total":"900"},
                        {"year":"2008-2009","total":"1000"},
                        {"year":"2009-2010","total":"1300"},
                        {"year":"2010-2011","total":"999"}]
         }]
}

I am trying to sort the datapoints into an array with the following format

var linedataline = [{dataPoints : [{ x: 1,
                                        y: 1300,
                                        label: "2007-2008"
                                    }, {
                                        x: 2,
                                        y: 1280,
                                        label: "2008-2009"
                                    }, {
                                        x: 3,
                                        y: 1100,
                                        label: "2009-2010"
                                    }, {
                                        x: 4,
                                        y: 1400,
                                        label: "2010-2011"
                                    }]
                                },
                                {dataPoints : [{ x: 1,
                                        y: 900,
                                        label: "2007-2008"
                                    }, {
                                        x: 2,
                                        y: 1000,
                                        label: "2008-2009"
                                    }, {
                                        x: 3,
                                        y: 1300,
                                        label: "2009-2010"
                                    }, {
                                        x: 4,
                                        y: 999,
                                        label: "2010-2011"
                                    }]

                            }]; 

I have used the following javascript

for(var i=0; i<data.label.length;i++){
                datapoints.length = 0;
                for(var j=0;j<data.label[i].datapoints.length;j++){
                    datapoints.push({
                                x: j+1,
                                y: data.label[i].datapoints[j].total,
                                label: data.label[i].datapoints[j].year
                    })
                }
                linedataline.push({ 
                        dataPoints: datapoints 
                })     
            }

where "data" is the parsed JSON. The problem is that linedataline does contain two arrays as required, but both print the second JSON array and not the first one. If I do not empty the datapoints array, it returns both the arrays but linedataline has array length = 8 and it does not separate the two arrays. What is the problem here?

1
  • 1
    That doesn't really have anything to do with sorting or JSON. You seem to want to convert an array of objects into a different format/structure. Commented Apr 14, 2014 at 6:23

2 Answers 2

1

replace datapoints.length = 0; with var datapoints = [];

var data = {"label":[{"name":"Government Schools",
          "datapoints":[{"year":"2007-2008","total":"1300"},
                        {"year":"2008-2009","total":"1280"},
                        {"year":"2009-2010","total":"1100"},
                        {"year":"2010-2011","total":"1400"}]
         },
         {"name":"Total Schools",
          "datapoints":[{"year":"2007-2008","total":"900"},
                        {"year":"2008-2009","total":"1000"},
                        {"year":"2009-2010","total":"1300"},
                        {"year":"2010-2011","total":"999"}]
         }]
};
var linedataline = [];
for(var i=0; i<data.label.length;i++){
                var datapoints = [];
                for(var j=0;j<data.label[i].datapoints.length;j++){
                    datapoints.push({
                                x: j+1,
                                y: data.label[i].datapoints[j].total,
                                label: data.label[i].datapoints[j].year
                    });
                }
                linedataline.push({ 
                        dataPoints: datapoints 
                });
            }
Sign up to request clarification or add additional context in comments.

Comments

1

You should create new instance.

for(var i=0; i<data.label.length;i++){
  var datapoints = [];
  ...
}

3 Comments

Yes I have it created. Havent mentioned it in the code here. I have created all the instances.
But you miss to create new instance for each datapoints in the loop.
Oh, I had all the arrays created outside the loops. I hadn't created an instance inside the loop. This seems to work. Thank you so much!

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.