How to merge object in IE 11

David picture David · Feb 7, 2017 · Viewed 49k times · Source

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?

Answer

Nebojsa Sapic picture Nebojsa Sapic · Aug 16, 2017

You can use jQuery method $.extend() which work in IE 11.

var object = {name: 'John', surname: 'Rowland'};
var newObject = $.extend({}, object);

newObject.age = '30';

console.log(object);
console.log(newObject)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>