我有以下格式的数据,基本上是一个对象数组,我已经尝试了几种方法,我如何才能更有效地做到这一点?
const overdue = [
{
"user.employeeId": "10001440",
"objectives": [
"Understand the financial indexes."
]
},
{
"user.employeeId": "10000303",
"objectives": [
"Document preparation & writing skills"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Chia ratio setting for Fuze Tea products L11"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Paramix Line 9 setting parameter standardization"
]
},
]如何使用lodash通过JavaScript将其转换为以下内容??
[
{
"user.employeeId": "10001440",
"objectives": [
"Understand the financial indexs."
]
},
{
"user.employeeId": "10000303",
"objectives": [
"Document preparation & writing skills"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization",
"Paramix Line 9 setting parameter standardization",
"Chia ratio setting for Fuze Tea products L11"
]
}
]我已经尝试过Array.map了!和正常的逻辑,我们如何使用lodash reduce或arr.reduce更有效地做到这一点呢?
发布于 2020-09-15 20:10:35
您可以使用Array.reduce来实现所需的结果,将数组用作累加器。
我们枚举过期数组中的每个对象,如果它不存在于输出数组中,我们添加它,否则我们将对象目标添加到输出中的相关元素。
const overdue = [ { "user.employeeId": "10001440", "objectives": [ "Understand the financial indexes." ] }, { "user.employeeId": "10000303", "objectives": [ "Document preparation & writing skills" ] }, { "user.employeeId": "10002168", "objectives": [ "Chia ratio setting for Fuze Tea products L11" ] }, { "user.employeeId": "10002168", "objectives": [ "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization" ] }, { "user.employeeId": "10002168", "objectives": [ "Paramix Line 9 setting parameter standardization" ] }, ];
let result = overdue.reduce((res, row) => {
let el = res.find(el => el["user.employeeId"] === row["user.employeeId"]);
// If we find the object in the output array simply update the objectives
if (el) {
el.objectives = [...el.objectives, ...row.objectives];
} else {
// If we _don't_ find the object, add to the output array.
res.push({ ...row});
}
return res;
}, [])
console.log("Result:",result);
发布于 2020-09-15 20:11:09
使用array.reduce并将对象传递给累加器。在回调函数中,检查累加器是否有与user.employeeId的值相同的键。如果不是,则创建键并将迭代下的当前对象添加为它的值。如果已经有一个键,那么只需更新objectives数组即可。当检索值为Object.value时,它将给出一个数组
const overdue = [{
"user.employeeId": "10001440",
"objectives": [
"Understand the financial indexes."
]
},
{
"user.employeeId": "10000303",
"objectives": [
"Document preparation & writing skills"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Chia ratio setting for Fuze Tea products L11"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Paramix Line 9 setting parameter standardization"
]
},
];
let newData = overdue.reduce((acc, curr) => {
if (!acc[curr['user.employeeId']]) {
acc[curr['user.employeeId']] = curr;
} else {
curr.objectives.forEach((item) => {
acc[curr['user.employeeId']]['objectives'].push(item)
})
}
return acc;
}, {});
console.log(Object.values(newData))
发布于 2020-09-15 20:18:12
我不确定如何在lodash中做到这一点,但下面是我只使用普通Javascript就能做到的方法:
const overdue = [{
"user.employeeId": "10001440",
"objectives": [
"Understand the financial indexes."
]
},
{
"user.employeeId": "10000303",
"objectives": [
"Document preparation & writing skills"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Chia ratio setting for Fuze Tea products L11"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"
]
},
{
"user.employeeId": "10002168",
"objectives": [
"Paramix Line 9 setting parameter standardization"
]
},
];
const combined = {};
overdue.forEach(o => {
const id = o["user.employeeId"];
const obj = o["objectives"][0];
if(!combined[id]) { combined[id] = [obj]; }
else { combined[id].push(obj); }
});
const result = [];
Object.keys(combined).forEach(key => {
result.push({"user.employeeId" : key, "objectives" : combined[key]});
});
console.log(result);
https://stackoverflow.com/questions/63901400
复制相似问题