如何根据对象数组更新复杂的 json 对象
2020-08-27
136
我有 2 个数据结构,如下所示
data1 :
{
purchaceOrder: [{
name: "Purchase Order",
version: 1,
description: "purchase order process",
saved: true,
visibility: true
},
{
name: "Purchase Order",
version: 2,
description: "purchase order process",
saved: false,
visibility: true
}
],
requestOrder: [{
name: "Request Order",
version: 1,
description: "request order process",
saved: true,
visibility: true
},
{
name: "Request Order",
version: 2,
description: "request order process",
saved: false,
visibility: true
}
],
cancelOrder: [{
name: "Cancel Order",
version: 1,
description: "cancel order process",
saved: false,
visibility: false
}]
}
data2:
[
{
id: "dwffrgefg68964",
name: "Purchase Order",
version: 1
},
{
id: "emffrgefg68964",
name: "Purchase Order",
version: 2
},
{
id: "iuffrgefg68964",
name: "request Order",
version: 1
}
]
我想根据名称过滤 data1,将 data2 中的 id 添加到 data1 中的每个对象,如果在 data2 中找不到进程名称,则删除 data1 中的整个对象/空数组,如下所示
最终结果:
{
purchaceOrder: [{
id: "dwffrgefg68964"
name: "Purchase Order",
version: 1,
description: "purchase order process",
saved: true,
visibility: true
},
{
id: "emffrgefg68964"
name: "Purchase Order",
version: 2,
description: "purchase order process",
saved: false,
visibility: true
}
],
requestOrder: [{
id: "iuffrgefg68964"
name: "Request Order",
version: 1,
description: "request order process",
saved: true,
visibility: true
}]
}
我尝试了各种解决方案,但无法获得预期的结果。这是我所得到的:
getAllProcess(){
for (let key in data1) {
var temp1 = data1[key];
for (let i = 0; i < temp1.length; i++) {
const reqModel = data2.find(process=> process.name === temp1[i].name&& process.version === temp1[i].version);
if(reqModel){
temp1[i].id=reqModel.id;
data1[key][i]=temp1[i];
}
}}
return data1;
}
2个回答
您的代码很接近;您只需要删除
data1
中在
data2
中没有对应值的条目:
let data1 = {
purchaseOrder: [{
name: "Purchase Order",
version: 1,
description: "purchase order process",
saved: true,
visibility: true
},
{
name: "Purchase Order",
version: 2,
description: "purchase order process",
saved: false,
visibility: true
}
],
requestOrder: [{
name: "Request Order",
version: 1,
description: "request order process",
saved: true,
visibility: true
},
{
name: "Request Order",
version: 2,
description: "request order process",
saved: false,
visibility: true
}
],
cancelOrder: [{
name: "Cancel Order",
version: 1,
description: "cancel order process",
saved: false,
visibility: false
}]
};
let data2 = [{
id: "dwffrgefg68964",
name: "Purchase Order",
version: 1
},
{
id: "emffrgefg68964",
name: "Purchase Order",
version: 2
},
{
id: "iuffrgefg68964",
name: "Request Order",
version: 1
}
];
function getAllProcess() {
for (let key in data1) {
var temp1 = data1[key];
for (let i = temp1.length - 1; i >= 0; i--) {
const reqModel = data2.find(process => process.name === temp1[i].name && process.version === temp1[i].version);
if (reqModel) {
temp1[i].id = reqModel.id;
data1[key][i] = temp1[i];
} else {
data1[key].splice(i, 1);
}
}
if (!data1[key].length) {
delete data1[key];
}
}
return data1;
}
console.log(getAllProcess());
Nick
2020-08-27
我为你制定了特定的算法。它使用空数组创建
data1
,然后根据
name
属性推送
data2
项。
const data1 = {
requestOrder: [],
purchaseOrder: [],
cancelOrder: []
};
const data2 = ...;
data2.forEach((d) => {
if (d.name == 'Request Order') {
data1.requestOrder.push(d);
} else if (d.name == 'Purcase Order') {
data1.purchaseOrder.push(d);
} else {
data1.cancelOrder.push(d);
}
});
Osman Omar
2020-08-27