arranging array objects in custom order

The solution for “arranging array objects in custom order” can be found here. The following code will assist you in solving the problem.

var sortOrder = [‘n’, ‘a’, ‘u’];
var myArray = [{
name: ‘u’
},
{
name: ‘n’
},
{
name: ‘a’
},
{
name: ‘n’
}
];
myArray.sort(function(a, b) {
return sortOrder.indexOf(a.name) – sortOrder.indexOf(b.name);
});

console.log(myArray);Save to mem.dev

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in