close

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to convert an Object {} to an Array [] of key-value pairs in JavaScript in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to convert an Object {} to an Array [] of key-value pairs in JavaScript?

  1. How to convert an Object {} to an Array [] of key-value pairs in JavaScript

    to convert an Object {} to an Array [] of key-value pairs in JavaScript If you are using lodash, it could be as simple as this:
    var arr = _.values(obj);

  2. How to convert an Object {} to an Array [] of key-value pairs in JavaScript

    to convert an Object {} to an Array [] of key-value pairs in JavaScript If you are using lodash, it could be as simple as this:
    var arr = _.values(obj);

Method 1

The best way is to do:

var obj ={"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10"‌​:0,"11":0,"12":0} 
Object.entries(obj);

Calling entries, as shown here, will return [key, value] pairs, as the asker requested.

Alternatively, you could call Object.values(obj), which would return only values.

Method 2

If you are using lodash, it could be as simple as this:

var arr = _.values(obj);

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment