Converting Json Name/value To Array With Object Literals
I'm looking to convert key-value JSON code that looks like this : { 'AK': 'Arkansas', 'NY': 'New York', 'CA': 'California' } Into an array, where the key value from th
Solution 1:
Use this code:
var obj = {
"AK": "Arkansas",
"NY": "New York",
"CA": "California"
}
var array1 = [];
for (key in obj) {
array1.push({
"code": key,
"name": obj[key]
});
}
console.log(array1);
Solution 2:
There are several ways, the most browser compatible is:
var p = [];
//Places would be your initial objectfor (x in places) {
p.push({code: x, name: places[x]})
}
Maybe you should use something like lodash to make things like this easier
Post a Comment for "Converting Json Name/value To Array With Object Literals"