RxJs groupBy 带有一组对象,每个对象都有键和值数组
RxJs groupBy with an array of objects each having key and values array
所以要求是这样的:
我得到了一个对象数组(例如 Array)
[
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
]
我将有一个函数接受 属性 的名称作为参数,员工列表将根据该名称进行分组。所以如果输入参数值为'empDept',那么我想要的结果如下:
[
{
key: 'Engineering',
values: [
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
]
},
{
key: 'HR',
values: [
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
]
},
{
key: 'Finance',
values: [
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
]
}
]
我正在使用 RxJs groupBy 运算符来实现此目的。我试过下面的代码,但它似乎没有给出想要的结果:
this.groupedEmployees$ = employees$.pipe(
groupBy(person => person.empDept),
mergeMap(group => of({ key: group.key, values: group.pipe(mergeMap(x => toArray())) })),
);
感谢您的帮助。
在这种情况下为什么要使用 rxjs?这纯粹是数组转换。你可以使用 lodash groupBy:https://lodash.com/docs/#groupBy
const t = [
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
];
const result = Object.entries(_.groupBy(t, 'empDept')).map(([key, values]) => ({key, values}));
您在找这样的东西吗?
function groupBy(key, array) {
return array.reduce((all, current) => {
const existingKey = all.find(existing => existing.key === current[key]);
if (!existingKey) {
all.push({key: current[key], values: [current]});
} else {
existingKey.values.push(current);
}
return all;
}, []);
}
示例:
const arr = [
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
];
const byDept = groupBy('empDept', arr);
const byLoc = groupBy('empLoc', arr);
如果你真的想坚持使用 RxJS,你可以使用这段代码:
import { from } from 'rxjs';
import { groupBy, mergeMap, reduce, toArray } from 'rxjs/operators';
const groupedEmployees$ = from([
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' }
]).pipe(
groupBy(person => person.empDept),
mergeMap(group => group
.pipe(
reduce((acc, cur) => {
acc.values.push(cur);
return acc;
},
{ key: group.key, values: [] }
)
)
),
toArray()
);
groupedEmployees$.subscribe(console.log);
所以要求是这样的: 我得到了一个对象数组(例如 Array)
[
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
]
我将有一个函数接受 属性 的名称作为参数,员工列表将根据该名称进行分组。所以如果输入参数值为'empDept',那么我想要的结果如下:
[
{
key: 'Engineering',
values: [
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
]
},
{
key: 'HR',
values: [
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
]
},
{
key: 'Finance',
values: [
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
]
}
]
我正在使用 RxJs groupBy 运算符来实现此目的。我试过下面的代码,但它似乎没有给出想要的结果:
this.groupedEmployees$ = employees$.pipe(
groupBy(person => person.empDept),
mergeMap(group => of({ key: group.key, values: group.pipe(mergeMap(x => toArray())) })),
);
感谢您的帮助。
在这种情况下为什么要使用 rxjs?这纯粹是数组转换。你可以使用 lodash groupBy:https://lodash.com/docs/#groupBy
const t = [
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
];
const result = Object.entries(_.groupBy(t, 'empDept')).map(([key, values]) => ({key, values}));
您在找这样的东西吗?
function groupBy(key, array) {
return array.reduce((all, current) => {
const existingKey = all.find(existing => existing.key === current[key]);
if (!existingKey) {
all.push({key: current[key], values: [current]});
} else {
existingKey.values.push(current);
}
return all;
}, []);
}
示例:
const arr = [
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' },
];
const byDept = groupBy('empDept', arr);
const byLoc = groupBy('empLoc', arr);
如果你真的想坚持使用 RxJS,你可以使用这段代码:
import { from } from 'rxjs';
import { groupBy, mergeMap, reduce, toArray } from 'rxjs/operators';
const groupedEmployees$ = from([
{ empDept: 'Engineering', empLoc: 'Pune', empName: 'John' },
{ empDept: 'Engineering', empLoc: 'Mumbai', empName: 'Harry' },
{ empDept: 'HR', empLoc: 'Pune', empName: 'Denis' },
{ empDept: 'Finance', empLoc: 'Mumbai', empName: 'Elvis' }
]).pipe(
groupBy(person => person.empDept),
mergeMap(group => group
.pipe(
reduce((acc, cur) => {
acc.values.push(cur);
return acc;
},
{ key: group.key, values: [] }
)
)
),
toArray()
);
groupedEmployees$.subscribe(console.log);