Using vanilla JavaScript and without lodash, what is the most efficient way to group an array of objects by value in the following manner?
For example if the input data is:
const data = [{year: "2021", name: "Rick"}, {year: "2020", name: "Joe"}, {year: "2021", name: "Sam"}, {year: "2019", name: "Sally"}, {year: "2019", name: "Jess"}];
and the output data is:
const result = [{year: "2021", data: [{year: "2021", name: "Rick"}, {year: "2021", name: "Sam"}]}, {year: "2020", data: [{year: "2020", name: "Joe"}]}, {year: "2019", data: [{year: "2019", name: "Sally"}, {year: "2019", name: "Jess"}]}]