У меня есть массив объектов
{
"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, и он работает совершенно нормально. но удобочитаемость совершенно плохая. Я хочу добиться этого, используя только Лодаша. Любая помощь?
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);