How to convert an array of objects to object with key value pairs

I want to convert an array of objects to object with key value pairs in javascript.

var arr=[{"name1":"value1"},{"name2":"value2"},...}];

How can i convert it to an object such as

{"name1":"value1","name2":"value2",...}

I want it to be supported in majority of browsers.


You could use Object.assign and a spread syntax ... for creating a single object with the given array with objects.

var array = [{ name1: "value1" }, { name2: "value2" }],
    object = Object.assign({}, ...array);
    
console.log(object);

You could run a reduce over the array and return a new object. But it is important to remember that if properties are the same they will be overwritten.

const newObject = array.reduce((current, next) => {
  return { ...current, ...next};
}, {})

If you are using es5 and not es6:

var newObject = array.reduce(function(current, next){
  return Object.assign({}, current, next);
}, {})