JavaScript- Convert an Object to JS Array
Last Updated :
25 Nov, 2024
Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScript’s primitive data types (Number, String, Boolean, null, undefined, and symbol).
Methods to convert the Objects to JavaScript Array:
1. Using Object.keys() and JavaScript map() Method
By using Object.keys(), we are extracting keys from the Object then this key is passed to the map() function which maps the key and corresponding value as an array.
javascript
let obj = { "1": 5, "2": 7, "3": 0, "4": 0, "5": 0 };
let result = Object.keys(obj).map(function (key) {
return [Number(key), obj[key]];
});
console.log(result)
Output[ [ 1, 5 ], [ 2, 7 ], [ 3, 0 ], [ 4, 0 ], [ 5, 0 ] ]
2. Using JavaScript Object.entries() Method
The Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually.
javascript
let obj = { "1": 5, "2": 7, "3": 0, "4": 0, "5": 0 };
let result = Object.entries(obj);
console.log(result)
Output[ [ '1', 5 ], [ '2', 7 ], [ '3', 0 ], [ '4', 0 ], [ '5', 0 ] ]
3. Using for...in loop
In JavaScript, a for...in loop iterates over an object's keys. By checking if each key is a direct property using hasOwnProperty, key-value pairs are pushed into an array. This approach efficiently converts an object to an array of key-value pairs.
JavaScript
let obj = { "1": 5, "2": 7, "3": 0, "4": 0, "5": 0 };
let result = [];
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
result.push([Number(key), obj[key]]);
}
}
console.log(result)
Output[ [ 1, 5 ], [ 2, 7 ], [ 3, 0 ], [ 4, 0 ], [ 5, 0 ] ]
4. Using Object.getOwnPropertyNames() Method
The Object.getOwnPropertyNames() method retrieves all own property names of an object. By combining it with Array.prototype.map(), we can create an array of key-value pairs by iterating over the property names and accessing their corresponding values from the object.
JavaScript
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.getOwnPropertyNames(obj);
const arr = keys.map(key => [key, obj[key]]);
console.log(arr);
Output[ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
5. Using Object.values() and Object.keys()
In this approach, we use Object.values() to get an array of values from the object and combine it with Object.keys() to create an array of key-value pairs.
JavaScript
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
const values = Object.values(obj);
const res = keys.map((key, index) => [key, values[index]]);
console.log(res);
Output[ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
Similar Reads
JavaScript - Convert an Array to an Object These are the following ways to convert an array to an Object:1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScriptlet a = [1, 2, 3,
2 min read
Convert an Array to JSON in JavaScript Given a JavaScript Array and the task is to convert an array to JSON Object. Below are the approaches to convert an array to JSON using JsvaScript: Table of ContentJSON.stringify() methodObject.assign() methodJSON.stringify() methodThe use of JSON is to exchange data to/from a web server. While send
2 min read
JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array
4 min read
How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
JavaScript - Convert String to Array Strings in JavaScript are immutable (cannot be changed directly). However, arrays are mutable, allowing you to perform operations such as adding, removing, or modifying elements. Converting a string to an array makes it easier to:Access individual characters or substrings.Perform array operations su
5 min read
How to Convert an Array of Objects to Map in JavaScript? Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon
3 min read
JavaScript - Create an Object From Two Arrays Here are the different methods to create an object from two arrays in JavaScript1. Using for-each loopThe arr.forEach() method calls the provided function once for each element of the array. JavaScriptconst a1 = ['name', 'age', 'city']; const a2 = ['Ajay', 25, 'New Delhi']; const res = {}; a1.forEac
3 min read
How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay
2 min read
Set to Array in JS or JavaScript This article will show you how to convert a Set to an Array in JavaScript. A set can be converted to an array in JavaScript in the following ways:1. Using Spread OperatorThe JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to a new variable in the
2 min read