I have this sample of code
a = [ { "apple" : 1 } , { "orange" : 2 } ]
and how would I change this to the following?
a = { "apple" : 1, "orange": 2 }
I have this sample of code
a = [ { "apple" : 1 } , { "orange" : 2 } ]
and how would I change this to the following?
a = { "apple" : 1, "orange": 2 }
You could use Object.assign
with spread syntax ...
for the objects.
var array = [{ apple: 1 }, { orange: 2 }],
object = Object.assign({}, ...array);
console.log(object);
ES5 with Array#reduce
and Object.keys
var array = [{ apple: 1 }, { orange: 2 }],
object = array.reduce(function (r, o) {
Object.keys(o).forEach(function (k) {
r[k] = o[k];
});
return r;
}, {});
console.log(object);
Use Object.assign.
var a = [ { "apple" : 1 } , { "orange" : 2 } ]
a = Object.assign({}, ...a);
console.log(a);