Convert 2D Array to Object using Map or Reduce in JavaScript Last Updated : 25 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Converting a 2D array to an object is a common task in JavaScript development. This process involves transforming an array where each element represents a key-value pair into an object where keys are derived from one dimension of the array and values from another. Problem Description:Given a 2D array where each inner array contains a key-value pair, we aim to convert it into an object where the keys are extracted from the first element of each inner array and the values from the second element. Example: Consider the following 2D array: const arr = [ ["apple", 3], ["banana", 2], ["orange", 5]];Output: { "apple": 3, "banana": 2, "orange": 5}Using reduce() MethodIn this approach we will first Initialize an empty object then Iterate over the 2D array using reduce() method and then extract the key and value from each inner array then assign the key-value pair to the accumulator object which will return the resulting object. Example: Transforming a 2D array into an object using the reduce method for concise data conversion. JavaScript const arr = [ ["apple", 3], ["banana", 2], ["orange", 5] ]; const objReduce = arr .reduce((acc, [key, value]) => { acc[key] = value; return acc; }, {}); console.log(objReduce); Output{ apple: 3, banana: 2, orange: 5 } Time Complexity: O(n), where n is the number of elements in the 2D array. Space Complexity: O(n) Using map() Method and Object.fromEntries() In this approach we use the map method to map each inner array to a new array where the key and value are swapped. It Uses the Object.fromEntries() to convert the mapped array into an object. Example: Converting a 2D array into an object map for efficient data representation. JavaScript const arr = [ ["apple", 3], ["banana", 2], ["orange", 5] ]; const objMap = Object .fromEntries(arr.map(([key, value]) => [key, value])); console.log(objMap); Output{ apple: 3, banana: 2, orange: 5 } Time Complexity: O(n), where n is the number of elements in the 2D array. Space Complexity: O(n) Comment More infoAdvertise with us Next Article Convert 2D Array to Object using Map or Reduce in JavaScript lunatic1 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Convert an Array into a String We have been given an array as an input that consists of various elements and our task is to convert this array of elements into a string by using JavaScript. Below we have added the examples for better understanding:Example: Input: arr= [1,2,3,4,5,6]Output: 1,2,3,4,5,6Table of ContentUsing arr.join 4 min read JavaScript Program to Split Map Keys and Values into Separate Arrays In this article, we are going to learn about splitting map keys and values into separate arrays by using JavaScript. Splitting map keys and values into separate arrays refers to the process of taking a collection of key-value pairs stored in a map data structure and separating the keys and values in 5 min read JavaScript Program to Transform Nested Array into Normal Array JavaScript allows us to convert nested arrays into a single, flat array using JavaScript. By simplifying nested structures, you can enhance the manageability and readability of your code. Example: Input: [[1, 2], [3, [4, 5]]] Output: [1, 2, 3, 4, 5]Below are the approaches to transform nested arrays 3 min read Implement Custom Array Flat method in JavaScript The flat() method in JavaScript is used to flatten nested arrays. By using that function we can directly convert any type of array into 1D array. These are the following approaches to implementing a custom array flat method in JavaScript: Table of Content Using Recursion approachUsing Iterative appr 2 min read How to Convert Array into Array of Objects using map() & reduce() in JavaScript? An array of objects can contain multiple objects with the same properties i.e. key-value pairs. But a map does not contain duplicate values which means if you convert an array of objects with duplicate objects into a map, it will contain only the unique key-value pairs. Below are the approaches to c 2 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 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 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 How to convert Object's array to an array using JavaScript ? Given an array of objects and the task is to convert the object values to an array with the help of JavaScript. There are two approaches that are discussed below: Approach 1:We can use the map() method and return the values of each object which makes the array. Example: html<!DOCTYPE HTML> 2 min read How to convert a plain object into ES6 Map using JavaScript ? The task is to convert a JavaScript Object into a plain ES6 Map using JavaScript. we're going to discuss a few techniques. To understand the difference between a map and an object please go through the Map vs Object in JavaScript article. Below are the following approaches to converting a plain obje 2 min read Like