Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  69] [ 2]  / answers: 1 / hits: 56870  / 7 Years ago, tue, february 7, 2017, 12:00:00

I have the following array of object:


Objs[0] = {Name : "ABC"};
Objs[1] = {Roll : 123}

I'm trying to make it as the following:


Objs {
Name : "ABC",
Roll : 123
}

I attempted to make it with the following code:


var Objs = [{
Name: "ABC"
}, {
Roll: 123
}];

console.log(
Object.assign.apply(null, [{}].concat(Objs)) // 1
)
or

console.log(
Object.assign({}, ...Objs) // 2
)

The problem is that this is not working in IE 11.


I get the errors:



Error for 1 : Unable to get property 'on' of undefined or null reference


Error for 2 : Object doesn't support property or method 'assign'



Any suggestions on how I should merge the object in IE 11?


More From » object

 Answers
241

IE11 does not support Object.assign.



You could iterate the array and the keys and take the values as new property of the result object.





var objs = [{ Name: ABC }, { Roll: 123 }],
result = objs.reduce(function (r, o) {
Object.keys(o).forEach(function (k) {
r[k] = o[k];
});
return r;
}, {});

console.log(result);




[#59040] Saturday, February 4, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katianatasham

Total Points: 293
Total Questions: 110
Total Answers: 103

Location: Saint Helena
Member since Mon, Jun 28, 2021
3 Years ago
;