Answers for "arranging array objects in custom order"

0

arranging array objects in custom order

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
Posted by: Guest on May-19-2021

Code answers related to "arranging array objects in custom order"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language