0

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 the JSON becomes one of the values in the object literals.

0 : {code :"AK", name:"Arkansas"},
1 : {code:"NY", name:"New York"},
2 : {code:"CA", name:"California}

Is there an easy way to do this?

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • 1
    `for .. in` loop is the best way to go for you. Try it. – VisioN Sep 02 '14 at 16:15
  • Out of curiosity, why do you want to do this? I ask because there may be a way to achieve your goal another way without having to do this step. – Surreal Dreams Sep 02 '14 at 16:16
  • why this question is downvoted ? the question is clearly wrote... – Charles-Antoine Fournel Sep 02 '14 at 16:16
  • I need an array for a combo box in extJS. ExtJS can only accept the data as an array. – Oliver Watkins Sep 02 '14 at 16:17
  • 1
    What have you tried so far? What are you having problems with? Do you have problems a) parsing the JSON? b) iterating over the object? c) creating a new array? d) creating a new object? e) adding elements to an array? – Felix Kling Sep 02 '14 at 16:17
  • @Charles-AntoineFournel - it may be downvoted because someone felt the poster didn't put in some research effort before posting. – Surreal Dreams Sep 02 '14 at 16:18
  • my thinking is that there may be a snappy/easy solution from say jquery. Otherwise i could probably figure it out with lots of loops etc. – Oliver Watkins Sep 02 '14 at 16:19
  • 2
    @OliverWatkins FYI, there is only one loop required to solve your problem. – VisioN Sep 02 '14 at 16:19
  • If using jQuery is an option, you should tag the question as such. And no, jQuery doesn't provide anything to help here. You just need one loop, not lots of loops ;) – Felix Kling Sep 02 '14 at 16:20
  • 1
    @OliverWatkins - this question is not a duplicate, but the accepted answer may be useful in solving your problem: http://stackoverflow.com/questions/14615669/convert-objects-properties-and-values-to-array-of-key-value-pairs – Surreal Dreams Sep 02 '14 at 16:21
  • It should be noted that there won't be any particular ordering to the resulting array unless you do something explicit to impose an ordering. There's no defined ordering for the properties of an object. – Pointy Sep 02 '14 at 18:06

2 Answers2

2

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);
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79
1

There are several ways, the most browser compatible is:

var p = [];
//Places would be your initial object
for (x in places) {
     p.push({code: x, name: places[x]})
}

Maybe you should use something like lodash to make things like this easier

Nickydonna
  • 802
  • 1
  • 5
  • 18