How to use Object.entries() In Javascript

JavaScript introduced the Object.entries() method with ECMAScript 2017 (ES8). This method returns an array of key and values of the operating object. This technique offers a clear alternative to using Object.keys() manually to complete a conversion.

Example: The below code will explain the use of object.entries() method to convert a dictionary into an array of objects.

Javascript
const dictionaryToArrayOfObjects = (dictionary) => {
    return Object.entries(dictionary).
        map(([key, value]) => ({ key, value }));
};

const myDictionary = {
    name: "John",
    age: 30,
    city: "New York"
};

const arrayOfObjects = 
    dictionaryToArrayOfObjects(myDictionary);
console.log(arrayOfObjects);

Output
[
  { key: 'name', value: 'John' },
  { key: 'age', value: 30 },
  { key: 'city', value: 'New York' }
]

Convert Dictionary into an Array of objects in JavaScript

In JavaScript, dictionaries also known as objects are used to store key-value pairs. But sometimes you might need to convert this data into an array of objects so that you can handle or utilize particular parts more easily.

Below are the multiple ways of converting a Dictionary into an Array of objects in JavaScript:

Table of Content

  • Using Object.keys() and map()
  • Using Object.entries()
  • Using for/in loop
  • Using Object.values() and map()

Similar Reads

Using Object.keys() and map()

One popular method for converting a dictionary into an array of objects is to map the dictionary keys using the map method into an array of objects by first extracting them using Object.keys()....

Using Object.entries()

JavaScript introduced the Object.entries() method with ECMAScript 2017 (ES8). This method returns an array of key and values of the operating object. This technique offers a clear alternative to using Object.keys() manually to complete a conversion....

Using for/in loop

Alternatively, you can manually iterate through the entries of the dictionary using a for/in loop and construct an array of objects....

Using Object.values() and map()

This approach leverages the Object.values() method, which returns an array of a given object’s own enumerable property values, and the Object.keys() method to construct an array of objects. This method is particularly useful when you only care about the values and want to include the corresponding keys in the result....

Contact Us