我有一个项目的主列表,这是一个嵌套的对象数组,我也有一个选定的列表。
let masterList = [{
category_id: 1,
meta_list: [{
name: "Cuisine",
id: "QWEQWEQWE",
is_multiselect: false,
item_list: ["Arabian", "Indian"]
}, {
name: "Cost for Two",
id: "SFDFSDASDASDASD",
is_multiselect: true,
item_list: [
"AED 0 - 100",
"AED 100 - 200",
"Greater Than AED 200"
]
}]
}, {
category_id: 2,
meta_list: [{
name: "Cat 2",
id: "cat2",
is_multiselect: false,
item_list: ["cat 2 1", "cat 2 2"]
}, {
name: "cuisine 2",
id: "cui2",
is_multiselect: true,
item_list: ["cu1", "cu2"]
}]
}];
let selectedList = [{
category_id: 1,
meta_list: [{
name: "Cuisine",
id: "QWEQWEQWE",
is_multiselect: false,
item_list: ["Arabian"]
}, {
name: "Cost for Two",
id: "SFDFSDASDASDASD",
is_multiselect: true,
item_list: [
"AED 100 - 200",
"Greater Than AED 200"
]
}]
}, {
category_id: 2,
meta_list: [{
name: "cuisine 2",
id: "cui2",
is_multiselect: true,
item_list: ["cu1"]
}]
}];
使用masterList和选择的列表,我想有一个namedList
必须看起来像这样
[{
category_id: 1,
meta_list: [{
name: "Cuisine",
id: "QWEQWEQWE",
is_multiselect: false,
item_list: ["Arabian", "Indian"],
sel_list: ["Arabian"]
}, {
name: "Cost for Two",
id: "SFDFSDASDASDASD",
is_multiselect: true,
item_list: [
"AED 0 - 100",
"AED 100 - 200",
"Greater Than AED 200"
],
sel_list: [
"AED 100 - 200",
"Greater Than AED 200"
]
}]
}, {
category_id: 2,
meta_list: [{
name: "Cat 2",
id: "cat2",
is_multiselect: false,
item_list: ["cat 2 1", "cat 2 2"],
sel_list: ["cat 2 2"]
}, {
name: "cuisine 2",
id: "cui2",
is_multiselect: true,
item_list: ["cu1", "cu2"],
sel_list: ["cu1"]
}]
}];
我希望具有与masterList相同的项和结构的期望列表,除了它具有另一个属性
sel_list
,其值将是item_list
数组中selected_list
的对应值。masterList
和selectedList
的区别在于item_list
中的selectedList
属性是item_list
中masterList
的子集如您在示例中所见,
masterList
也可以有一个附加项{
name: "Cat 2",
id: "cat2",
is_multiselect: false,
item_list: ["cat 2 1", "cat 2 2"],
}
[{
category_id: 1,
meta_list: [{
name: "Cuisine",
id: "QWEQWEQWE",
is_multiselect: false,
item_list: ["Arabian", "Indian"],
sel_list: ["Arabian"]
}, {
name: "Cost for Two",
id: "SFDFSDASDASDASD",
is_multiselect: true,
item_list: [
"AED 0 - 100",
"AED 100 - 200",
"Greater Than AED 200"
],
sel_list: [
"AED 100 - 200",
"Greater Than AED 200"
]
}]
}, {
category_id: 2,
meta_list: [{
name: "Cat 2",
id: "cat2",
is_multiselect: false,
item_list: ["cat 2 1", "cat 2 2"],
sel_list: ["cat 2 2"]
}, {
name: "cuisine 2",
id: "cui2",
is_multiselect: true,
item_list: ["cu1", "cu2"],
sel_list: ["cu1"]
}]
}]
不在
selectedList
中。我该如何实现?
编辑:我目前正在这样
masterList.forEach(cat_met_item => {
selectedList.forEach(filled_cat_met_item => {
if (cat_met_item.category_id === filled_cat_met_item.category_id) {
cat_met_item.meta_list.forEach(met_item => {
filled_cat_met_item.meta_list.forEach(filled_met_item => {
if (met_item.id === filled_met_item.id) {
met_item["list"] = filled_met_item["item_list"];
}
});
});
}
});
});
使用4个forEach循环。我发现我的方法效率不高且非常凌乱,所以有更好的方法吗?
最佳答案
使用json过滤器方法:
let masterList = [
{
category_id: 1,
meta_list: [
{
name: "Cuisine",
id: "QWEQWEQWE",
is_multiselect: false,
item_list: ["Arabian", "Indian"]
},
{
name: "Cost for Two",
id: "SFDFSDASDASDASD",
is_multiselect: true,
item_list: [
"AED 0 - 100",
"AED 100 - 200",
"Greater Than AED 200"
]
}
]
},
{
category_id: 2,
meta_list: [
{
name: "Cat 2",
id: "cat2",
is_multiselect: false,
item_list: ["cat 2 1", "cat 2 2"]
},
{
name: "cuisine 2",
id: "cui2",
is_multiselect: true,
item_list: ["cu1", "cu2"]
}
]
}
];
let selectedList = [
{
category_id: 1,
meta_list: [
{
name: "Cuisine",
id: "QWEQWEQWE",
is_multiselect: false,
item_list: ["Arabian"]
},
{
name: "Cost for Two",
id: "SFDFSDASDASDASD",
is_multiselect: true,
item_list: [
"AED 100 - 200",
"Greater Than AED 200"
]
}
]
},
{
category_id: 2,
meta_list: [
{
name: "cuisine 2",
id: "cui2",
is_multiselect: true,
item_list: ["cu1"]
}
]
}
];
masterList.forEach(cat_met_item => {
var obj = selectedList.filter(function (el) {
return cat_met_item.category_id === el.category_id;
})
cat_met_item.meta_list.forEach(met_item => {
var selObj = obj[0].meta_list.filter(function (el) {
return met_item.id === el.id;
});
if (selObj[0]) {
met_item['sel_list'] = selObj[0].item_list;
}
else {
met_item['sel_list'] = met_item.item_list[met_item.item_list.length - 1];
}
});
});
console.log(masterList);