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);
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.You can use this function to sort the object
Example:
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.
Sorry I had to add it again and make it long. Let me if you have any other problem with the solution.