Michael
Michael

Reputation: 7377

Jquery merge two fields in JSON object

I have a JSON object in JQuery that looks like:

["state":"06","population":9},{"state":"32","population":13},{"state":"01","population":4},

I want to end up with a JSON object like:

{"06":9,"32":13,"01":4}

How would I do this in JQuery?

Thanks!

Upvotes: 0

Views: 1085

Answers (1)

rjz
rjz

Reputation: 16510

you would want to use the .each method. If my_array holds the original array you describe, this ought to do it:

var result = {};
$.each(my_array, function(index, val) {
  result[val.state] = val.population;
});

Upvotes: 1

Related Questions