4 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以使用Array.reduce來(lái)獲得所需的結(jié)果,使用數(shù)組作為累加器。
我們枚舉過(guò)期數(shù)組中的每個(gè)對(duì)象,如果它不存在于輸出數(shù)組中,我們將其添加,否則我們將對(duì)象目標(biāo)添加到輸出中的相關(guān)元素。
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);

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
使用array.reduce對(duì)象并將其傳遞給累加器。在回調(diào)函數(shù)中檢查累加器是否具有與 的值相同的鍵user.employeeId。如果不是,則創(chuàng)建鍵并將迭代中的當(dāng)前對(duì)象添加為它的值。如果已經(jīng)有一個(gè)鍵,那么只需更新目標(biāo)數(shù)組。在檢索值時(shí)Object.valuewhich 將給出一個(gè)數(shù)組
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))

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
我不確定如何在 lodash 中執(zhí)行此操作,但這是我僅使用 vanilla Javascript 執(zhí)行此操作的方法:
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);

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
如果你可以使用 lodash,那么你就可以超越 reduce 并使用更具體的東西來(lái)滿足你的需求。在這種情況下,您可以考慮使用該_.groupBy
方法,該方法將創(chuàng)建一個(gè)由user.employeeId
值作為鍵的對(duì)象,其中包含值作為每個(gè)對(duì)象的數(shù)組。然后,您可以將對(duì)象的值(即:具有相同 employeeId 的對(duì)象數(shù)組)映射到每個(gè)數(shù)組合并的合并對(duì)象objectives
。最后,您可以獲取分組對(duì)象的值以獲得結(jié)果:
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 group = _.flow(
arr => _.groupBy(arr, "user.employeeId"),
g => _.mapValues(g, arr => _.mergeWith(...arr, (objV, srcV) => {
if (_.isArray(objV)) return objV.concat(srcV);
})),
_.values
);
console.log(group(overdue));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
或者您可以將 vanilla JS 與.reduce()
a 一起使用Map
,這使用了 JS 的更多現(xiàn)代功能(節(jié)點(diǎn) 14.0.0 均支持),例如可選鏈接 ?.
和空合并運(yùn)算符 ??
:
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 group = (arr, key) => Array.from(arr.reduce((m, obj) =>
m.set(
obj[key],
{...obj, objectives: [...(m.get(obj[key])?.objectives ?? []), ...obj.objectives]}
), new Map).values()
);
console.log(group(overdue, "user.employeeId"));
添加回答
舉報(bào)