I'm using Angular 14.
I have an Observable that is an Array of Objects, looks like a dictionary with key/values.
Array: [
0: Object {id: 100, manufacturer: 'ford', model: 'mustang'},
1: Object {id: 110, manufacturer: 'ford', model: 'fiesta'},
2: Object {id: 150, manufacturer: 'ford', model: 'escort'},
3: Object {id: 320, manufacturer: 'Toyota', model: 'camry'},
4: Object {id: 325, manufacturer: 'Toyota', model: 'rav4'},
5: Object {id: 345, manufacturer: 'Toyota', model: 'corolla'},
]
I want to group this array by manufacturer. I need two separate lists for Ford and Toyota.
I have read about the RxJs .groupBy() but I can't get it to work as this is a dictionary.
this.cars$.pipe(
groupBy(g => g[??????].manufacturer),
mergeMap(group => group.pipe(toArray()))
).subscribe(s => { .... });
I have also read about the Javascript reduce() function but it is difficult to understand and I can't find an example of it reducing a dictionary.
Any help appreciated!
To group your array of objects by manufacturer, you can use the reduce() method to create a new object with keys for each manufacturer and values as arrays of cars.
Here's an example implementation:
this.cars$.pipe(
map(cars => cars.reduce((acc, car) => {
if (!acc[car.manufacturer]) {
acc[car.manufacturer] = [];
}
acc[car.manufacturer].push(car);
return acc;
}, {})),
).subscribe(manufacturers => {
const fordCars = manufacturers['ford'];
const toyotaCars = manufacturers['Toyota'];
// Do something with the grouped arrays
});
Explanation:
Hope this helps!