In JavaScript, you can convert a 2D array into an object using various approaches. One common approach is to use the reduce()
method or a simple loop to iterate through the 2D array and construct an object.
Methods to Convert 2D Array to Object
- Using reduce()
- Using a Simple Loop
- Using Object.fromEntries()
Using reduce()
const arr = [
["name", "John"],
["age", 30],
["city", "New York"]
];
const obj = arr.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
console.log(obj);
Output of the above will be
{ name: 'John', age: 30, city: 'New York' }
In this example, we used reduce() method. The initial value of the accumulator (acc) is an empty object {}
. The reduce()
the function iterates through each element of the 2D array.
For each sub-array, it destructures the key
and value
and assigns the value
to the key
in the accumulator object.
Using a Simple Loop
const arr = [
["name", "John"],
["age", 30],
["city", "New York"]
];
const obj = {};
for (const [key, value] of arr) {
obj[key] = value;
}
console.log(obj);
Output of the above will be
{ name: 'John', age: 30, city: 'New York' }
This method uses a for…of loop to iterate through the 2D array.
During each iteration, the loop destructures the current sub-array into key and value. It then assigns the value to the key in an initially empty object (obj)
Using Object.fromEntries()
const arr = [
["name", "John"],
["age", 30],
["city", "New York"]
];
const obj = Object.fromEntries(arr);
console.log(obj);
Output of the above will be
{ name: 'John', age: 30, city: 'New York' }
The Object.fromEntries() method transforms a list of key-value pairs (as arrays) into an object.
In this method, the 2D array is directly passed to Object.fromEntries(). It creates an object from the sub-arrays, where the first element of each sub-array becomes the key and the second element becomes the value.
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 digits can be obtained by… 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 the spread operator + reverse(). Using… 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 two arrays. The problem Write… 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 – Using reduce() In the… Read more: JavaScript – Convert an array to an object
- JavaScript – Shuffle an array in a random orderTo shuffle an array in random order in JavaScript, you can use the sort() function with Math.random() or use the map() & sort() function. Let’s… Read more: JavaScript – Shuffle an array in a random order