Explain Codes LogoExplain Codes Logo

How to convert an Object {} to an Array

javascript
object-to-array
javascript-features
data-manipulation
Anton ShumikhinbyAnton Shumikhin·Nov 22, 2024
TLDR

To convert an Object to an Array of key-value pairs, use Object.entries():

const obj = { x: 1, y: 2 }; const arr = Object.entries(obj); // [['x', 1], ['y', 2]]

Object.entries(obj) instantly transforms obj's properties into [key, value] pairs within arr.

An in-depth look into Object to Array transformation

JavaScript provides multiple ways to convert objects into arrays. It depends on the specific requirements you have, the nature of the objects, and how you want to manipulate the data.

Transforming keys to array

When dealing with object keys, you can use Object.keys() and map() to create a key-value array. Here's an example:

// Who needs keys anyway, we're not locksmiths const obj = { x: 1, y: 2 }; const arr = Object.keys(obj).map(key => [key, obj[key]]); // [['x', 1], ['y', 2]]

Comments joking aside, map() allows you to transform and control the array contents during conversion.

When keys are not strings

If you assume that "keys are always strings", take a seat and let this next piece of code enlighten you:

const obj = { "1": "one", "2": "two" }; const arr = Object.entries(obj).map(([k, v]) => [+k, v]); // [[1, 'one'], [2, 'two']]

Key type preserved. Mind blown.

Keeping memory in check

When dealing with large objects, you need to consider memory usage. Use Object.assign() within map() to modify the array without using additional memory:

// Squeezing out every last byte of memory, because why not let obj = { x: 1, y: 2 }; let arr = Object.keys(obj).map(key => Object.assign([], { [key]: obj[key] })); // [{ x: 1 }, { y: 2 }]

Applying data manipulation techniques

JavaScript is not just about converting types. It's about manipulating data in the most effective way possible. Let's go further with some advanced manipulation methods.

Get rid of duplicates

A combination of map() and spread operator can help filter out duplicates:

const obj = { x: 1, y: 1, z: 2 }; // Secret function to vanish duplicates const arr = Object.entries(obj).reduce((acc, [k, v]) => { if (!acc.find(a => a[1] === v)) acc.push([k, v]); return acc; }, []); // [['x', 1], ['z', 2]]

Let's do some math with our values

If we need to aggregate our object values or apply mathematical transformations:

const obj = { x: 1, y: 2, z: 3 }; // Quick mafs const sum = Object.values(obj).reduce((total, value) => total + value, 0); // sum is 6. Quick, mafs!

Embracing modern JavaScript

We're living in times of ECMAScript 6 (ES6) for a reason. Make use of the modern sleek JS features like Object.entries() to make your code tidy, yet supercharged.

Leveraging the Object to Array conversion

With data processing and manipulation tasks at hand, it's critical to evaluate the best methods and practices for conversion.

Staying friendly with data processing requirements

For further data analysis and complex functionalities, conversion to arrays is a smart move. You open the gate for a plethora of array methods like sort, filter, and reduce.

Preserving the order

In cases where the key order or uniqueness is crucial, you can have creative mapping and filtering techniques to maintain data integrity throughout the conversion process.