1

I want to convert an Object to an array. The object is like this {1: 36, 3: 112, 6: 71} and i want it to convert it to this [[1,36],[3,112],[6,71]]. Actually the data is JSON then i want to use it as data for jqplot.

I have found answers as to converting object to array but mostly like this: from {1: 36, 3: 112, 6: 71} to [[36],[112],[71]].

Any ideas regarding this one? Any help will be appreciated.

2 Answers 2

5

A simple solution :

var arr = [];
for (var k in obj) arr.push([+k, obj[k]]); 

+k is used to convert the key from a string (all object keys are strings) to a number.

Sign up to request clarification or add additional context in comments.

1 Comment

/OT How can I see new user's posts? Is there any way or do I have to wait?
3

You can use Object.keys and [].map

var arr = Object.keys(obj).map(function(k){ return [+k, obj[k]];  });

+k is same as Number(k) as Object.keys return an Array of String, but you want it in Number

2 Comments

I got it with this answer also. Thanks
@elL ah! Glad to have helped :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.