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);
}, {})
With modern JS (YMMV):
const arr = [{name1:"value1"}, {name2:"value2"}, {a:1,b:2}]; const obj = Object.fromEntries(arr.flatMap(Object.entries)); console.log(obj);
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 );
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)
I had a different starting array:
let arr = [{key:"name1",value:"value1"},{key:"name2",value:"value2"}]
This was my solution:
let kvp = Object.assign({}, ...arr.map(item => ({[item.key]:item.value})))
console.log(kvp) // {"name1":"value1","name2":"value2"}
None of the other answers worked for me. If you want to convert an array of objects into an object of key-value pair, like so:
[{ key:"url", value:"new" },{ key:"page", value:1 }] // before
{ url:"new", page:1 } // after
But I figured out an elegant one-liner thanks to this article by Chris Burgen :
let array = [{ key:"url", value:"new" },{ key:"page", value:1 }]
let kvp = Object.assign({}, ...array.map(item => ({[item.key]:item.value})))
console.log(kvp) // {url: "new", page: 1}
Try this:
var arr=[{"name1":"value1"},{"name2":"value2"}];
var new_arr=[];
$.each(arr,function(index){
$.each(arr[index],function(key,value){
var nw_obj={};
nw_obj[key]=value;
new_arr.push(nw_obj);
});
});
console.log(JSON.stringify(new_arr));
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.