gpt4 book ai didi

javascript - 根据另一个数组中的值删除嵌套对象数组中的值

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

我有一个具有以下结构的对象数组:

let optionList = [
{
images: [
{
url: "test1"
},
{
url: "test2"
}
]
},
{
images: [
{
url: "test3"
},
{
url: "test4"
}
]
},
{
images: [
{
url: "test5"
},
{
url: "test6"
}
]
}
];

我还有另一个名为 imagesToDelete 的数组,其值如下:

 let imagesToDelete = ["test1", "test3", "test6"];

我的目标是根据imagesToDelete 数组中的值从嵌套数组删除 。如果操作正确,这将是以下结果:

let optionList = [
{
images: [
{
url: "test2"
}
]
},
{
images: [
{
url: "test4"
}
]
},
{
images: [
{
url: "test5"
}
]
}
];

下面是我当前的代码,它没有删除任何值:

 optionList.filter(ol => {
let result = !ol.images.some(
image => image.url === imagesToDelete.includes(image.url)
);
return result;
});

console.log(optionList);

最佳答案

let optionList = [{
images: [{
url: "test1"
},
{
url: "test2"
}
]
},
{
images: [{
url: "test3"
},
{
url: "test4"
}
]
},
{
images: [{
url: "test5"
},
{
url: "test6"
}
]
}
];
let imagesToDelete = ["test1", "test3", "test6"];
let newOptionList = optionList.map(function(option) {
option.images = option.images.filter(function(item) {
return !imagesToDelete.includes(item.url)
})
return option
})

console.log('newOptionList', newOptionList)

关于javascript - 根据另一个数组中的值删除嵌套对象数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57100533/

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