[Solved] How to group array of objects by value [duplicate]


You want to examine all the objects in your input array, then push it into an array identified by the first letter of the name in your object.

var input = [{
    name: "John",
    id: "123"
  },
  {
    name: "Josh",
    id: "1234"
  },
  {
    name: "Charlie",
    id: "1234253"
  },
  {
    name: "Charles",
    id: "123345"
  }
];
var result = {};

input.forEach(x => !!result[x.name[0]] ? result[x.name[0]].push(x) : result[x.name[0]] = [x]);
console.log(result);

solved How to group array of objects by value [duplicate]