gpt4 book ai didi

javascript - 如何使用纯 Javascript (ES6) 按 id 合并两个 json 对象值

转载 作者:行者123 更新时间:2023-11-30 09:38:41 24 4
gpt4 key购买 nike

我有两个 Javascript 对象数组,我想将两个具有相同“ObjID”的 Javascript 对象的属性合并到一个 Javascript 对象中。

例如:

// Array of Javascript Objects 1:
let request1 = [{"ObjId":174864,"ObjMutationD":"2010-07-09T00:00:00.000Z","ObjMitarbeiterS":"epf","ObjAufId":142},
{"ObjId":175999,"ObjMutationD":"2010-07-09T00:00:00.000Z","ObjMitarbeiterS":"epf","ObjAufId":149}]

// Array of Javascript Objects 2:
let request2 = [{"ObjId":174864,"MulPfadS":"M:\\Originalbilder\\FGS\\95nn","MulDateiS":"9576.305-034-1","MulExtentS":"jpg"},
{"ObjId":177791,"MulPfadS":"M:\\Originalbilder\\FGS\\95nn","MulDateiS":"9576.305-035-1","MulExtentS":"jpg"}]

合并到一个具有相同 ObjId 值的 Javascript 对象(参见 ObjID:174864),其余(对象)保持不变:

let result = [{"ObjId":174864,"ObjMutationD":"2010-07-09T00:00:00.000Z","ObjMitarbeiterS":"epf","ObjAufId":142,
"MulPfadS":"M:\\Originalbilder\\FGS\\95nn","MulDateiS":"9576.305-034-1","MulExtentS":"jpg"},
{"ObjId":175999,"ObjMutationD":"2010-07-09T00:00:00.000Z","ObjMitarbeiterS":"epf","ObjAufId":149},
{"ObjId":177791,"MulPfadS":"M:\\Originalbilder\\FGS\\95nn","MulDateiS":"9576.305-035-1","MulExtentS":"jpg"}]

到目前为止我尝试了什么:

let request1 = [{"ObjId":174864,"ObjMutationD":"2010-07-09T00:00:00.000Z","ObjMitarbeiterS":"epf","ObjAufId":142},
{"ObjId":175999,"ObjMutationD":"2010-07-09T00:00:00.000Z","ObjMitarbeiterS":"epf","ObjAufId":149}]

let request2 = [{"ObjId":174864,"MulPfadS":"M:\\Originalbilder\\FGS\\95nn","MulDateiS":"9576.305-034-1","MulExtentS":"jpg"},
{"ObjId":177791,"MulPfadS":"M:\\Originalbilder\\FGS\\95nn","MulDateiS":"9576.305-035-1","MulExtentS":"jpg"}]

function joinObjects() {
var idMap = {};
for(let i = 0; i < arguments.length; i++) {
for(let j = 0; j < arguments[i].length; j++) {
let currentID = arguments[i][j]['ObjId'];
if(!idMap[currentID]) {
idMap[currentID] = {};
}
// Iterate over properties of objects in arrays
for(key in arguments[i][j]) {
idMap[currentID][key] = arguments[i][j][key];
}
}
}

// push properties of idMap into an array
let newArray = [];
for(property in idMap) {
newArray.push(idMap[property]);
}
return newArray;
}

let result = joinObjects(request1, request2);

最佳答案

你可以使用 MapObject.assign用于合并对象。

var request1 = [{
ObjId: 174864,
ObjMutationD: "2010-07-09T00:00:00.000Z",
ObjMitarbeiterS: "epf",
ObjAufId: 142
}, {
ObjId: 175999,
ObjMutationD: "2010-07-09T00:00:00.000Z",
ObjMitarbeiterS: "epf",
ObjAufId: 149
}],
request2 = [{
ObjId: 174864,
MulPfadS: "M:\\Originalbilder\\FGS\\95nn",
MulDateiS: "9576.305-034-1",
MulExtentS: "jpg"
}, {
ObjId: 177791,
MulPfadS: "M:\\Originalbilder\\FGS\\95nn",
MulDateiS: "9576.305-035-1",
MulExtentS: "jpg"
}];

var result = [...[request1, request2].reduce((m, a) => (a.forEach(o => m.has(o.ObjId) && Object.assign(m.get(o.ObjId), o) || m.set(o.ObjId, o)), m), new Map).values()];

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何使用纯 Javascript (ES6) 按 id 合并两个 json 对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42206177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com