How to convert an array of objects to an object in JavaScript ?

In this article we will learn about how to convert an array of objects to an object in JavaScript.
The simple and the easiest way to merge an array of objects into an single object with all its key / value pairs is by using the Object.assign()
method along with the spread operator (...
).
The Object.assign() method copies all enumerable and own properties of two or more sources into the target object and then return us the target object.
Object.assign({}, sources)
Let us see an example on how we can covert arrays of objects to a single object with all the properties of the merged objects.
let Arr = [{ animal : 'Monkey' }, { place: 'Amazon' }, { food: 'Banana' }]
const animalDetail = Object.assign({}, ...Arr)
console.log(animalDetail)
OUTPUT:
{ animal: 'Monkey', place: 'Amazon', food: 'Banana' }
IMPORTANT: If two objects have same property name then the later one will overwrite the previous one.
Related Topic:
How to merge two or more objects into one object in JavaScript?
Convert an array to object in JavaScript?