0

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.

1
  • 5
    please show us what you've tried Commented Dec 31, 2015 at 14:16

1 Answer 1

3
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;
Sign up to request clarification or add additional context in comments.

6 Comments

Object.keys is not supported in IE8 and below
@AdamAzad Where did you find the restriction that IE8 has to be supported? I can't find one...
@AdamAzad IE8 is an unsupported browser. Unless specified, answers don't have to cater to legacy browser versions.
@AdamAzad I think Andreas is asking where OP says needs IE8 support in Question
Well you can use for-in in that case.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.