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
- JavaScript – Sum of Digits of a NumberIn this article, we will learn how to find the sum of digits of a given number. The sum of… Read more: JavaScript – Sum of Digits of a Number
- JavaScript – Reverse a StringIn this article, we’ll look at three basic ways to reverse a string in JavaScript: the built-in reverse() method, a for loop, and… Read more: JavaScript – Reverse a String
- JavaScript – Find the Intersection of Two ArraysIn this article, we will explore how to implement a function in JavaScript to find the intersection (Common Elements) of… Read more: JavaScript – Find the Intersection of Two Arrays
- JavaScript – Convert an array to an objectThere are many ways to convert an array to an object in JavaScript. Let’s explore some of the common approaches… Read more: JavaScript – Convert an array to an object