From an array of objects, I would like to add an alumni
attribute for each object. The value of this attribute is a concatenation separated by "-" of the names of all the objects that have the same school
value.
Now the tricky part is that I need to make it performance efficient as possible, as the initial object array is very large.
I figured out a solution using groupBy school, in each group map the different names of and concat them into a string, then looping on each object to add the new attribute and value, and finally loop on each group to place all the objects in a new array (do undo the groups).
I am certain that there is a much cleaner way to do this. Any suggestion is more than welcome.
Below is the initial array, and the expected result with the new attribute added to each object in the array.
var initialArr = [
{name:"A", school:"LFM"},
{name:"B", school:"LFM"},
{name:"C", school:"PBE"},
{name:"D", school:"LFM"},
{name:"E", school:"BPE"},
{name:"F", school:"LFM"}
];
var expectedResult = [
{name:"A", school:"LFM", alumni:"A-B-D-F"},
{name:"B", school:"LFM", alumni:"A-B-D-F"},
{name:"C", school:"PBE", alumni:"C-E"},
{name:"D", school:"LFM", alumni:"A-B-D-F"},
{name:"E", school:"BPE", alumni:"C-E"},
{name:"F", school:"LFM", alumni:"A-B-D-F"}
];