s sharma s sharma - 6 months ago 11
Javascript Question

Convert some part of json to arry using JavaScript or jQuery

I am getting issue and want to convert it into array manually using JavaScript.

{
"code" :"1",
"data" : {
"0" : {"id":"1","score":"44"},
"1" : {"id":"1","score":"44"}
}
}


I want above that in this format:

{
"code": "1",
"data": [
{"id": "1","score": "44"},
{"id": "2"," score": "45"}
]
}


I tried using Remove element from JSON Object and Converting JSON Object into Javascript array.

However, it is not getting up to the mark, so I'm still trying my best. If somebody has solution please let me know.

Answer
var json =  {
   "code" :"1", 
   "data" : { 
    "0" : {"id":"1","score":"44"},
    "1" : {"id":"1","score":"44"}
    }
};

var dataArray = Object.keys(json.data).map(function (i) {
    return json.data[i];
});

json.data = dataArray;