Converting JavaScript object with numeric keys into array
It's actually very straight forward with jQuery's $.map
var arr = $.map(obj, function(el) { return el });
and almost as easy without jQuery as well, converting the keys to an array and then mapping back the values with Array.map
var arr = Object.keys(obj).map(function(k) { return obj[k] });
That's assuming it's already parsed as a javascript object, and isn't actually JSON, which is a string format, in that case a run through JSON.parse
would be necessary as well.
In ES2015 there's Object.values
to the rescue, which makes this a breeze
var arr = Object.values(obj);
var json = '{"0":"1","1":"2","2":"3","3":"4"}';var parsed = JSON.parse(json);var arr = [];for (var x in parsed) { arr.push(parsed[x]);}console.log(arr)
Hope this is what you're after!
You simply do it like
var data = { "0": "1", "1": "2", "2": "3", "3": "4"};var arr = [];for (var prop in data) { arr.push(data[prop]);}console.log(arr);