0

I need to separate an array of objects by selected property, that came dynamically from server, my first step is to identify the quantity of labels that came in response, then i need to separate it into sets of object, based on the quantity of labels of the object ( using it to render a apex graphic bar on angular.)

I need to separate a array by categories, the api is returning something like this:

[ 
{name: 'Alex', classroom: "A", Age: 25}, 
{name: 'Rose', classroom: "A", Age: 24}, 
{name: 'Peter', classroom: "B", Age: 21}, 
{name: 'Alex', classroom: "C", Age: 22}
]

I need a output like this:

[
{classroom: "A", name: ["Alex, Rose"], Age: [25, 24]}
{classroom: "B", name: ["Peter"], Age: [21] },
{classroom: "C", name: ["Alex"], Age: [22] },
]

The use case is a lot complex than that, but the main idea is separate items by property.

What could i do to achieve something like that?

mrflash818
  • 930
  • 13
  • 24
  • Wouldn't an object as an output make more sense? – kelsny Mar 13 '23 at 21:21
  • or the canonical [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – pilchard Mar 13 '23 at 21:33
  • But I agree with vr. surely you actually want `[{ classroom: 'A', students: [{ name: 'Alex', Age: 25 }, { name: 'Rose', Age: 24 },], }, ...];` (which is also covered by the duplicate) – pilchard Mar 13 '23 at 21:34

0 Answers0