javascript - 如何使用 lodash 按嵌套属性分组?
问题描述
我有对象数组
{
"work": [{
"_id": "5c80c5c00c253823fc443337",
"start": "2019-01-01T18:30:00.000Z",
"end": "2019-01-02T18:30:00.000Z",
"employee": {
"_id": "5c80c16e0c253823fc44332a",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890"
},
"allocation": 30
},
{
"_id": "5c80c5ef0c253823fc443339",
"start": "2018-12-31T18:30:00.000Z",
"end": "2019-09-30T18:30:00.000Z",
"employee": {
"_id": "5c80c16e0c253823fc44332a",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890"
},
"allocation": 100
},
{
"_id": "5c80c60b0c253823fc44333a",
"start": "2018-12-31T18:30:00.000Z",
"end": "2020-10-07T18:30:00.000Z",
"employee": {
"_id": "5c80c16e0c253823fc44332a",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890"
},
"allocation": 25
},
{
"_id": "5c80c65e0c253823fc44333b",
"start": "2019-01-01T18:30:00.000Z",
"end": "2019-10-04T18:30:00.000Z",
"employee": {
"_id": "5c80c1940c253823fc44332b",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890"
},
"allocation": 50
},
{
"_id": "5c80c7240c253823fc44333f",
"start": "2018-12-31T18:30:00.000Z",
"end": "2019-10-09T18:30:00.000Z",
"employee": {
"_id": "5c80c26e0c253823fc44332e",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890"
},
"allocation": 25
}
]
}
我需要将它们转换为
[{
"_id": "5c80c16e0c253823fc44332a",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890",
"work": [{
"_id": "5c80c5c00c253823fc443337",
"start": "2019-01-01T18:30:00.000Z",
"end": "2019-01-02T18:30:00.000Z",
"allocation": 30
}, {
"_id": "5c80c5ef0c253823fc443339",
"start": "2018-12-31T18:30:00.000Z",
"end": "2019-09-30T18:30:00.000Z",
"allocation": 100
}, {
"_id": "5c80c60b0c253823fc44333a",
"start": "2018-12-31T18:30:00.000Z",
"end": "2020-10-07T18:30:00.000Z",
"allocation": 25
}]
}, {
"_id": "5c80c1940c253823fc44332b",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890",
"work": [{
"_id": "5c80c65e0c253823fc44333b",
"start": "2019-01-01T18:30:00.000Z",
"end": "2019-10-04T18:30:00.000Z",
"allocation": 50
}]
}, {
"_id": "5c80c26e0c253823fc44332e",
"status": "active",
"location": "Chennai",
"contact_number": "1234567890",
"work": [{
"_id": "5c80c7240c253823fc44333f",
"start": "2018-12-31T18:30:00.000Z",
"end": "2019-10-09T18:30:00.000Z",
"allocation": 25
}]
}]
我通过部分使用 lodash 和 vanilla js 完成了它,它工作得很好。但可读性完全不好。我想单独使用 lodash 来实现这一点。有什么帮助吗?
let ids: any = groupBy(this.project.work, function (res) {
return res.employee._id;
});
for (let id in ids) {
let tmp = [];
let employee_added = false;
ids[id].map((work) => {
if (!employee_added) {
tmp = work.employee;
tmp['work'] = [];
employee_added = true;
}
delete work.employee;
tmp['work'].push(work);
})
this.employees.push(tmp);
}
console.log(this.employees);
解决方案
希望这是您正在寻找的。
- 首先将其分组
employee_.id
- 然后映射每个组,取第一个员工(因为每个组必须有一个条目)
- 然后将每个组的所有其他成员(并获取外部部分)映射到
work
(除了员工对象之外的所有内容)
这是下面的示例:
let works = [{"_id":"5c80c5c00c253823fc443337","start":"2019-01-01T18:30:00.000Z","end":"2019-01-02T18:30:00.000Z","employee":{"_id":"5c80c16e0c253823fc44332a","status":"active","location":"Chennai","contact_number":"1234567890"},"allocation":30},{"_id":"5c80c5ef0c253823fc443339","start":"2018-12-31T18:30:00.000Z","end":"2019-09-30T18:30:00.000Z","employee":{"_id":"5c80c16e0c253823fc44332a","status":"active","location":"Chennai","contact_number":"1234567890"},"allocation":100},{"_id":"5c80c60b0c253823fc44333a","start":"2018-12-31T18:30:00.000Z","end":"2020-10-07T18:30:00.000Z","employee":{"_id":"5c80c16e0c253823fc44332a","status":"active","location":"Chennai","contact_number":"1234567890"},"allocation":25},{"_id":"5c80c65e0c253823fc44333b","start":"2019-01-01T18:30:00.000Z","end":"2019-10-04T18:30:00.000Z","employee":{"_id":"5c80c1940c253823fc44332b","status":"active","location":"Chennai","contact_number":"1234567890"},"allocation":50},{"_id":"5c80c7240c253823fc44333f","start":"2018-12-31T18:30:00.000Z","end":"2019-10-09T18:30:00.000Z","employee":{"_id":"5c80c26e0c253823fc44332e","status":"active","location":"Chennai","contact_number":"1234567890"},"allocation":25}]
let res = _(works)
.groupBy('employee._id')
.map(g => ({...g[0].employee, work: _.map(g, ({employee, ...rest}) => ({...rest}))}))
.value();
console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
推荐阅读
- apache-spark - 如何通过 Spark 运行 HDFS 文件系统命令?
- python - 在python中记录所有http请求
- node.js - EJS条件导入不显示html
- java - 2.1 企业 bean 上的资源引用标记
- python-3.x - 如何使用 asyncio 和 aiohttp 进行循环而不是 for 循环?
- wordpress - 关闭无限滚动
- javascript - aync await 是否与 React setState 方法一起使用?
- kubernetes - 卡夫卡内存泄漏
- python-3.x - Pandas,字符串 case 语句到数据框 - 两个以上的条件
- for-loop - 如何处理 CTE 中的所有记录