英文:
GroupBy or Reduce on Array of Objects Observable
问题
I'm using Angular 14.
我正在使用 Angular 14。
I have an Observable that is an Array of Objects, looks like a dictionary with key/values.
我有一个Observable,它是一个对象数组,看起来像一个带有键/值的字典。
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'},
]
数组:[
0: 对象 {id: 100, 制造商: '福特', 型号: '野马'},
1: 对象 {id: 110, 制造商: '福特', 型号: '嘉年华'},
2: 对象 {id: 150, 制造商: '福特', 型号: '护航'},
3: 对象 {id: 320, 制造商: '丰田', 型号: '凯美瑞'},
4: 对象 {id: 325, 制造商: '丰田', 型号: 'RAV4'},
5: 对象 {id: 345, 制造商: '丰田', 型号: '卡罗拉'},
]
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.
我了解了RxJs的groupBy()方法,但由于这是一个字典,我无法使其正常工作。
this.cars$.pipe(
groupBy(g => g[??????].manufacturer),
mergeMap(group => group.pipe(toArray()))
).subscribe(s => { .... });
这个代码示例中的 "??????" 部分应该替换为您想要使用的属性名称,例如 "manufacturer"。
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.
我也了解了JavaScript的reduce()函数,但它很难理解,而且我找不到它减少字典的示例。
Any help appreciated!
任何帮助都会受到欢迎!
英文:
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!
答案1
得分: 2
要将您的对象数组按制造商分组,可以使用reduce()方法创建一个新对象,其中每个制造商都有一个键,值为汽车数组。
以下是一个示例实现:
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'];
// 对分组后的数组执行进一步操作
});
解释:
- 使用**map()**操作符将原始的汽车数组Observable转换为一个新的Observable,它发出一个具有制造商作为键和汽车数组作为值的对象。
- 使用**reduce()**方法迭代数组中的每辆车,并将其添加到累加器对象中相应制造商的数组中。
- 累加器对象从一个空对象**{}**开始。
- 如果制造商数组尚不存在,则会用一个空数组进行初始化。
- 在每次迭代结束时,累加器对象acc被返回,从而构建最终的分组对象。
- 使用**subscribe()**方法来消耗结果对象并提取Ford和Toyota的数组,可以用于进一步处理。
希望这有所帮助!
英文:
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:
- map() operator is used to transform the original Observable of cars array into a new Observable that emits an object with manufacturers as keys and arrays of cars as values.
- reduce() method is used to iterate through each car in the array and add it to the corresponding manufacturer array in the accumulator object.
- The accumulator object starts as an empty object {}.
- If the manufacturer array doesn't exist yet, it's initialized with an empty array.
- The acc (accumulator) object is returned at the end of each iteration, which builds up the final grouped object.
- The subscribe() method is used to consume the resulting object and extract the arrays for Ford and Toyota, which can be used for further processing.
Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论