Anjana Choudhary Anjana Choudhary - 2 months ago 20
Ajax Question

convert ajax response into a particular format?

enter image description here Input:

var result= JSON.parse(response);
console.log(result);


Given below is the output of console.log(result); Now i want to convert this into given below output i want like this.

Output:
{day: Array(7), amount: Array(7)}
amount: (7) [413, 14, 2, 37, 50, 22, 82]
day: (7) ["02-Jan-16", "03-Jan-16", "04-Jan-16", "05-Jan-16", "06-Jan-16",
"07-Jan-16", "08-Jan-16"]


This input is ajax response data. Now i want to convert this response data
into a particular format which i describe below. My output should look like
this. How can i do this?


Output I want:

[[02-Jan-16,413],[03-Jan-16,14],[04-Jan-16,2],[05-Jan-16,37], [06-Jan-
16,50],[07-Jan-16,22],[08-Jan-16,82]]


enter image description here
This is the output image. check it out.

Answer Source

You could map the wanted values for a new array.

function getData(object) {
    return data.amount.map(function(a, i) {
        return [object.day[i], a];
    });
}

var data = {
        amount: [413, 14, 2, 37, 50, 22, 82],
        day: ["02-Jan-16", "03-Jan-16", "04-Jan-16", "05-Jan-16", "06-Jan-16", "07-Jan-16", "08-Jan-16"]
    },
    result = getData(data);
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }