DEV Community

Cover image for How to convert objects into arrays in Javascript
Arika O
Arika O

Posted on • Edited on

22 7

How to convert objects into arrays in Javascript

Gone are the days when transforming Javascript objects into arrays required complex logic. Starting with E2017(ES8), we can do just that pretty easily. Depending on our needs, we can convert an object into an array containing the keys, the values or the whole key-value pairs. Let's see how this works. We are going to use the Object class and some specific static methods found on this class. These are: Object.keys(), Object.values() and Object.entries().

// We have an object
const pairs = {
  Key1: "value1",
  Key2: "value2",
  Key3: "value3"
}

// Converting the object into an array holding the keys
const arrayOfKeys = Object.keys(pairs);
console.log(arrayOfKeys);// prints [Key1, Key2, Key3]

// Converting the object into an array holding the values
const arrayOfValues = Object.values(pairs);
console.log(arrayOfValues);// prints [value1, value2, value3]

// Converting the object into an array holding the key-value pairs
const arrayOfCompleteEntries = Object.entries(pairs);
console.log(arrayOfCompleteEntries);// prints [[Key1, value1], [Key2, value2], [Key3, value3]];
Enter fullscreen mode Exit fullscreen mode

As we can see, in the last example where we are extracting the key-value pairs we end up with an array of arrays. If we want to work with each key or value from the original object, we can use forEach to loop through every sub-array:

arrayOfCompleteEntries.forEach(([key, value]) => {
  console.log(key);
  console.log(value);
})

// prints 
Key1
value1
Key2
value2
Key3
value3
Enter fullscreen mode Exit fullscreen mode

If for some reason we want to convert the newly created array back into an object, we can do that very easily like this:

const backToObject = Object.fromEntries(arrayOfCompleteEntries);
console.log(backToObject);

// prints
{
Key1:"value1",
Key2:"value2",
Key3:"value3"
}
Enter fullscreen mode Exit fullscreen mode

Image source: Adeolu Eletu/ @adeolueletu on Unsplash

Top comments (4)

Collapse
 
rsa profile image
Ranieri Althoff

Very nice, I use these a lot since I learned about it.

Object.fromEntries is pretty useful. I use it in place of [...].reduce((prev, [key, value]) => ({ ...prev, [key]: value }), {}) for much cleaner syntax and better Typescript inference :D

Collapse
 
arikaturika profile image
Arika O

Funny, just yesterday I flattened an array using reduce() and I ran into Typescript issues because of the inference. Still didn't find a solution :D. But yes, these methods are super useful.

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thanks silvia :)

Collapse
 
arikaturika profile image
Arika O

Hope it helped :).

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay