Convert json array to javascript array

I have a json array that I want to convert to a simple javascript array:

This is my json array:

var users = {"0":"John","1":"Simon","2":"Randy"} 

How to convert it to a simple javascript array as follows:

 var users = ["John", "Simon", "Randy"] 
+8
json javascript
source share
3 answers

users already a JS object (not JSON). But here you go:

 var users_array = []; for(var i in users) { if(users.hasOwnProperty(i) && !isNaN(+i)) { users_array[+i] = users[i]; } } 

Edit: Insert elements at the correct position in the array. Thanks @RoToRa .

It might be easier not to create such an object in the first place. How is this created?

+10
source share

Just for fun - if you know the length of the array, then the following will work ( and seems to be faster ):

 users.length = 3; users = Array.prototype.slice.call(users); 
+4
source share

Well, here is a jQuery + Javascript solution for those who are interested:

 var user_list = []; $.each( users, function( key, value ) { user_list.push( value ); }); console.log(user_list); 
0
source share

All Articles