JavaScript – Convert an object to an array

There are many ways to convert an object to an array in JavaScript. Let’s explore some common approaches.

Convert Object Values to Array

If you want to extract just the values from an object and create an array, you can use Object.values()

const obj = { a: 1, b: 2, c: 3 };
const valuesArray = Object.values(obj);
console.log(valuesArray); // [1, 2, 3]

Convert Object Keys to Array

If you want to extract just the keys from an object and create an array, you can use Object.keys()

const obj = { a: 1, b: 2, c: 3 };
const keysArray = Object.keys(obj);
console.log(keysArray); // ["a", "b", "c"]

Convert Object Entries to Array

If you want to convert both keys and values into an array of key-value pairs, you can use Object.entries()

const obj = { a: 1, b: 2, c: 3 };
const entriesArray = Object.entries(obj);
console.log(entriesArray); // [["a", 1], ["b", 2], ["c", 3]]

Similar Reads