1

How can I combine following three or more objects and sorting them by the key "name" alphabetically?

var obj1 = {name: "BBBB", number: 2, task: "testing"};
var obj2 = {name: "AAAA"};
var obj3 = {name: "CCCC", number: 1};
Hello
  • 13
  • 3
  • 2
    What do you exactly mean by combine? Will they have the one `name`property? Sharing the wanted output, and the code you tried will be helpful. – cнŝdk Jul 03 '17 at 10:35

1 Answers1

1

var obj1 = {
  name: "BBBB",
  number: 2,
  task: "testing"
};
var obj2 = {
  name: "AAAA"
};
var obj3 = {
  name: "CCCC",
  number: 1
};

var obj4 = [obj1, obj2, obj3];
obj4.sort((a, b) => a.name.localeCompare(b.name));
console.log(obj4);
Blue
  • 22,608
  • 7
  • 62
  • 92
Meteor Newbie
  • 646
  • 2
  • 10
  • 23