Milos Milos - 3 months ago 26
JSON Question

How to parse json arrays into array rather than object

I have following foobar variable which is string(I get it like so by querying a webserver with $

var foobar = "[{param1: 'value1'}, {param2: 'value2'}]";

How do I make an array out of that? If I use JSON.parse, it will provide me with an object(tried
typeof foobar
). But, if I then use
delete param1
, it will then make it [null, {param2: 'value2'}]. What I want to do, basically is to remove first or second item. Also, value may be different, but keys will always stay the same, ie. they'll be numbers.


Seems like you're wanting to parse your array into a single object, from your delete statement. If so...

var foobar = '[{"param1":"value1"},{"param2":"value2"}]';
var json = JSON.parse(foobar);

json = json.reduce(function(previousValue, currentValue){
    previousValue[key] = currentValue[key];
  return previousValue;
}, {});


I could be wrong though.