How to convert two-dimensional array into an object in JavaScript ? - GeeksforGeeks

16 Apr, 2021
In this article, we will learn how to convert a two-dimensional array to an object. A two-dimensional array can have any number of rows and two columns.
Example:
Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
The below approaches can be followed to solve the problem.
Approach 1: In this approach, we create an empty object and use the Array.forEach() method to iterate over the array. On every iteration, we insert the first item of the child array into the object as a key and the second item as the value. Then it returns the object after the iterations.
 
Example:
Javascript
function arr2obj(arr) {      // Create an empty object    let obj = {};      arr.forEach((v) => {          // Extract the key and the value        let key = v[0];        let value = v[1];          // Add the key and value to        // the object        obj[key] = value;    });      // Return the object    return obj;}  console.log(    arr2obj([        ["John", 12],        ["Jack", 13],        ["Matt", 14],        ["Maxx", 15],    ]));
Output:
{
  Jack: 13,
  John: 12,
  Matt: 14,
  Maxx: 15
}
Approach 2: In this approach, we will use the Array.reduce() method and initialize the accumulator with an empty object. On every iteration, we assign the current value as the key’s value of the accumulator and return the accumulator. Then it returns the object after the iterations.
Example:
Javascript
function arr2obj(arr) {    return arr.reduce(        (acc, curr) => {                          // Extract the key and the value            let key = curr[0];            let value = curr[1];              // Assign key and value            // to the accumulator            acc[key] = value;              // Return the accumulator            return acc;        },          // Initialize with an empty object        {}    );}  console.log(    arr2obj([        ["Eren", "Yeager"],        ["Mikasa", "Ackermann"],        ["Armin", "Arlelt"],        ["Levi", "Ackermann"],    ]));
Output:
{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}
Approach 3: In this approach, we first flatten the array using the Array.flat() method so that we get a one-dimensional array. We can then create an empty object and iterate the array to assign evenly positioned values as the key of the object and oddly positioned values as the value.
Example:
Javascript
function arr2obj(arr) {      // Flatten the array    arr = arr.flat();      // Create an empty object    let obj = {};      for (let i = 0; i < arr.length; i++) {        if (i % 2 == 0) {              // Extract the key and the value            let key = arr[i];            let value = arr[i + 1];              // Assign the key and value            obj[key] = value;        }    }      return obj;}  console.log(    arr2obj([        ["Max", 19],        ["Chloe", 20],        ["Nathan", 22],        ["Mark", 31],    ]));
Output:
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
javascript-array
JavaScript-Methods
javascript-object
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript