0

I have the following json object and I wanted to get the desired output like below.

var testdata = [{
        "x": [
            "Jan-10",
            "Feb-10",
            "Mar-10",
            "Apr-10",
            "May-10"
        ],
        "y": [
            "100.0",
            "25.0",
            "100.0",
            "60.0",
            "500.0"
        ],
        "xf": "Jan-10",
        "xl": "May-10",
        "yf": "100.0",
        "yl": "500.0",
        "xtitle": "day",
        "ytitle": "rs"
    },
    {
        "x": [
            "Jan-11",
            "Feb-11",
            "Mar-11",
            "Apr-11",
            "May-11"
        ],
        "y": [
            "450.0",
            "650.0",
            "300.0",
            "70.0",
            "360.0"
        ],
        "xf": "Jan-11",
        "xl": "May-11",
        "yf": "450.0",
        "yl": "360.0",
        "xtitle": "day",
        "ytitle": "rs"
    }
];

I wanted to iterate this and should get the similar output like this(y1 and y2 are nothing but their values, those keys can be anything):

[{
        "day": "Jan",
        "y1": "100",
        "y2": "450"
    },
    {
        "day": "Feb",
        "y1": "25",
        "y2": "650"
    },
    {
        "day": "Mar",
        "y1": "100",
        "y2": "300"
    },
    {
        "day": "Apr",
        "y1": "60",
        "y2": "70"
    },
    {
        "day": "May",
        "y1": "500",
        "y2": "360"
    }
]

I have tried like:

if(testdata.length > 0){
         var outarr = [];
          for(var i = 0; i < testdata[0].x.length; i++) {
            outarr.push(
            {
            "x": testdata[0].x[i],
            "y": testdata[0].y[i]
            })
            }
        console.log(JSON.stringify(outarr));
}

but I am not getting from the above looping to get the actual output, please let me know how to do this to get my similar output. Created Fiddle. Thanks in advance !

Sana
  • 23
  • 6
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Sep 24 '17 at 12:02
  • It's not clear how your first input parameters got mapped to the desired result – Thusitha Sep 24 '17 at 12:03
  • There's nothing in your quoted code that produces objects with properties called `day`, `y1`, or `y2`. So that would be where to start. – T.J. Crowder Sep 24 '17 at 12:04
  • @T.J.Crowder, I have edited post, it means y1, y2 can be any keys, but my values should get like above. I need just similar output, that's it. – Sana Sep 24 '17 at 12:08
  • @Thusitha, I don't want eact output, need just similar output. Please note that keys(y1, y2 are can be anything, but their values should be correct), Edited post. – Sana Sep 24 '17 at 12:09

1 Answers1

1

Assuming the structure of testdata is consistent.

var testdata = [{
        "x": [
            "Jan-10",
            "Feb-10",
            "Mar-10",
            "Apr-10",
            "May-10"
        ],
        "y": [
            "100.0",
            "25.0",
            "100.0",
            "60.0",
            "500.0"
        ],
        "xf": "Jan-10",
        "xl": "May-10",
        "yf": "100.0",
        "yl": "500.0",
        "xtitle": "day",
        "ytitle": "rs"
    },
    {
        "x": [
            "Jan-11",
            "Feb-11",
            "Mar-11",
            "Apr-11",
            "May-11"
        ],
        "y": [
            "450.0",
            "650.0",
            "300.0",
            "70.0",
            "360.0"
        ],
        "xf": "Jan-11",
        "xl": "May-11",
        "yf": "450.0",
        "yl": "360.0",
        "xtitle": "day",
        "ytitle": "rs"
    }
];

if(testdata){
    months = testdata[0]['x'];


    output = [];
    for(var i = 0; i < months.length; i++){
        month = months[i].split('-')[0]
        output.push({
            "month" : month,
            "y1"    : testdata[0]['y'][i],
            "y2"    : testdata[1]['y'][i]
        });
    }

    console.log(output)
}

If you ever have more than the two entries, you can of course account for this:

if(testdata){
    months = testdata[0]['x'];
    numentries = testdata.length;

    output = [];
    for(var i = 0; i < months.length; i++){
        month = months[i].split('-')[0]

        monthData = {
            "month" : month
        }

        for(var x = 0; x < numentries; x++){
            monthData['y'+x] = testdata[x]['y'][i]
        }

        output.push(monthData);
    }

    console.log(output)
}
X33
  • 1,310
  • 16
  • 37