Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

I want to convert an object that looks like this:

{"1":72,"2":12,"2":91};

into an array of key-value pairs like this:

[[1,72],[2,12],[2,91]].

How can I convert an Object to an Array of key-value pairs in JavaScript?

1 Answer

0 votes
by (13.1k points)

To convert an object to an array of key-value pairs in javascript, you can use Object.keys() and map() to do it. For example:

Var object={“1”:72,”2”:12,”3”:91};

Var result=Object.keys(object).map((key)=>[Number(key),object[key]);

console.log(result);

 Want to be a Web Developer? Check out web developer certification from Intellipaat.

Related questions

0 votes
0 answers
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...