object.assign not working on IE

2.3k views Asked by At

I am trying to sort a set object data with custom sorting. After research, I find that object.assign can doing this. This code worked fine on Chrome but it showing syntax error on IE10/11. Is there any other methods can solve this? Thank you.

var obj = {
  "name4": [{
    "area": "area4"
  }],
  "name2": [{
    "area": "area2"
  }],
  "name1": [{
    "area": "area1"
  }],
  "name3": [{
    "area": "area3"
  }]
};

console.log(obj);

var tempObj = Object.assign(...['name1', 'name2', 'name3', 'name4'].map(k => ({
  [k]: obj[k]
})));

console.log(tempObj);

1

There are 1 answers

0
oluwasetemi On

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. It should be used for cloning,merging objects.

Here is a polyfill for Object.assign from MDN.

if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
 Object.defineProperty(Object, "assign", {
 value: function assign(target, varArgs) { // .length of function is 2
  'use strict';
  if (target == null) { // TypeError if undefined or null
    throw new TypeError('Cannot convert undefined or null to object');
  }

  var to = Object(target);

  for (var index = 1; index < arguments.length; index++) {
    var nextSource = arguments[index];

    if (nextSource != null) { // Skip over if undefined or null
      for (var nextKey in nextSource) {
        // Avoid bugs when hasOwnProperty is shadowed
        if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
          to[nextKey] = nextSource[nextKey];
        }
      }
    }
  }
  return to;
},
writable: true,
configurable: true
});
}

You can use this function to sort the object

const orderBy = (arr, props, orders) =>
  [...arr].sort((a, b) =>
    props.reduce((acc, prop, i) => {
      if (acc === 0) {
      const [p1, p2] = orders && orders[i] === 'desc' ? [b[prop], a[prop]] : 
      [a[prop], b[prop]];
      acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
     }
    return acc;
  }, 0)
);

Example:

`const users = [{ name: 'fred', age: 48 }, { name: 'barney', age: 36 }, { name: 'fred', age: 40 }];`

`orderBy(users, ['name', 'age'], ['asc', 'desc']); // [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]`

`orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]`

Thanks I hope this will be helpful. Keep coding the right way!

TL:DR; I worked on the object you posted and did some adjustment.

const obj1 = [{"name":"area1", "id": "4"},{"name":"area2", "id": "2"},
{"name":"area3", "id":"1"},{"name": "area4", "id": "3"}];

console.log(orderBy(obj1, ['id'], ['asc']));
console.log(orderBy(obj1, ['id'], ['desc']));
console.log(orderBy(obj1, ['name'], ['asc']));
console.log(orderBy(obj1, ['name'], ['desc']));

Sorry I had to add it again and make it long. Let me if you have any other problem with the solution.