36

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.

0

7 Answers 7

78

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);

Sign up to request clarification or add additional context in comments.

1 Comment

There are a few good uses for Object.assign even with object spread being available, and this is one of them.
7

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);
}, {})

1 Comment

This is very inefficient (quadratic time, lots of intermediate objects), which can be fixed by using Object.assign(current, next) instead of Object.assign({}, current, next).
6

With modern JS (YMMV):

  1. Split each object into entries
  2. Aggregate all entries into one object

const arr = [{name1:"value1"}, {name2:"value2"}, {a:1,b:2}];
const obj = Object.fromEntries(arr.flatMap(Object.entries));
console.log(obj);

1 Comment

Object.fromEntries and Array::flatMap are both slow
3

Try this simple logic

var arr=[{"name1":"value1"},{"name2":"value2"}];
var obj = {}; //create the empty output object
arr.forEach( function(item){ 
   var key = Object.keys(item)[0]; //take the first key from every object in the array
   obj[ key ] = item [ key ];  //assign the key and value to output obj
});
console.log( obj );

1 Comment

For short arrays and ES5 env, this seems the most readable/simple way
3

use with Array#forEach and Object.keys

var arr = [{"name1": "value1"},{"name2": "value2"}];
var obj = {};
arr.map(k => Object.keys(k).forEach(a => obj[a] = k[a]))
console.log(obj)

2 Comments

A map method returns a new array, however this function is just being used to mutate var obj so a forEach is more appropriate.
Object.keys(k).forEach(a => obj[a] = k[a]) is a slower, less readable, and less compatible version of Object.assign(obj, k).
-1

Using for...in loop :

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

var obj = {};
for (var i in arr) {
  obj[Object.keys(arr[i])] = arr[i][Object.keys(arr[i])];
}

console.log(obj);

Using Array.map() method with ES6 :

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

var obj = {};

arr.map(item => obj[Object.keys(item)] = item[Object.keys(item)]);

console.log(obj);

Using Object.assign() method with ES6 spreaqd(...) assignment :

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

let obj = Object.assign({}, ...arr);

console.log(obj);

Comments

-1

Supposing that you have myObject and myArray

myObject = {};
myArray = [
    {itemOneKey: 'itemOneValue', itemTwoKey: 'itemTwoValue'},
    {itemThreeKey: 'itemThreeValue'}
];

simply try this:

myArray.map(obj => {
  Object.keys(obj).map(key => myObject[key] = obj[key])
});

const myObject = {};
const myArray = [
    {itemOneKey: 'itemOneValue', itemTwoKey: 'itemTwoValue'},
    {itemThreeKey: 'itemThreeValue'}
];

myArray.map(obj => {
  Object.keys(obj).map(key => myObject[key] = obj[key])
});

console.log(myObject);

2 Comments

Object.keys(obj).map(key => myObject[key] = obj[key]) is a slower, less readable, and less compatible version of Object.assign(myObject, obj).
Who told you? Show me where it is said in the specification

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.