Convert Array to object in JavaScript

In this post, we are going to learn how to convert Array to object in JavaScript and ES by using different method or operator,spread operator,reduce() method,assign(),object.entries(),foreach() and for loop.

  • Using Spread operator
  • Using Object .assign()
  • Using Array.reduce()
  • Using Object.entries()
  • Using Foreach loop
  • Using For loop

1. Spread operator to Convert Array to object in JavaScript


The spread operator is a new operator introduced in the ES6 Standard of JavaScript. It is represented by three dots(…) . The spread operator takes an iterate object(Array, Set, Map, String) and Object and spread their element. So in this example, we are spreading the element of array to convert to an object

const array = ['Jack', 'Back', 'Rack'];
console.log({...array});

Output

{ '0': 'Jack', '1': 'Back', '2': 'Rack' }

2.Object.assign() to Convert Array to object in JavaScript


The object.assign() method copies values of the given object to the target object. In this javascript program, we will learn how to convert array to object in javascript by passing the array to assign method and get the object.

objarr = Object.assign({}, ['Jack', 'Back', 'Rack']);
console.log(objarr);

Output

{ '0': 'Jack', '1': 'Back', '2': 'Rack' }

3. reduce() to Convert Array to object in JavaScript


The reduce() method calls the user-defined reducer callback function on each element of the array and returns a single accumulated value. In this example we are using reduce() to convert an array to an object.

//one way 
objarr =(['Jack', 'Back', 'Rack']).reduce((key, val) => ({ ...key, [val]: val}), {}) 
 console.log('One way :',objarr);

//second way
 
objarray = ['Jack', 'Back', 'Rack'].reduce(function(result, item, index, array) {
  result[index] = item;
  return result;
}, {})

 console.log('\n Second way :',objarray );

Output

//one way

{ Jack: 'Jack', Back: 'Back', Rack: 'Rack' }

Second way:

{ '0': 'Jack', '1': 'Back', '2': 'Rack' }

4. object.entries() to Convert Array to object in JavaScript


“as per MDN, The object.entries() method returns an array of a given object’s own enumerable string-keyed property [key, value] pairs. This is the same as iterating with a for….in loop, except that a for….in loop enumerates properties in the prototype chain as well.”

const arr = ['Jack', 'Back', 'Rack'];
const objArray = Object.fromEntries(Object.entries(arr));

console.log(objArray);

Output

{ '0': 'Jack', '1': 'Back', '2': 'Rack' }

5. foreach loop to convert Array to object in JavaScript


In this javascript program, we are iterating through the element of the array and creating an object from the array element.

var resarr = {}

const arr = ['Jack', 'Back', 'Rack'];
arr.forEach((prop,index)=>resarr[index] = prop);
console.log(resarr);

output

{ '0': 'Jack', '1': 'Back', '2': 'Rack' }

6. For loop to Convert Array to object in JavaScript


In this javascript program we are iterating through elements of the given array and assigning all elements into the empty object and finally printing the created object from the array.

const arr = ['Jack', 'Back', 'Rack']
obj = {};

for (let i=0; i<arr.length; i++) {
   obj[i] = arr[i];
}
console.log(obj)

Output

{ '0': 'Jack', '1': 'Back', '2': 'Rack' }