javascript - 根据另一个数组中的值删除嵌套对象数组中的值
问题描述
我有一个具有以下结构的对象数组:
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)
推荐阅读
- php - 有没有办法独立于 Limesurvey 订阅来提高每月回复?
- javascript - SVG进度圈图像旋转方向
- php - Laravel 密码确认返回 ValidationException
- oracle - 表的循环列的Oracle结构
- java - Thymeleaf + Spring boot - 使用getter方法显示图片
- python - Python 上一行值
- c# - 为什么 Html.Action 在找不到控制器时失败?
- asp.net - 错误:在 gridview 中更新多行时索引超出范围
- docker - Fluent-bit 客户端和 Fluentd 服务器之间的 TLS 问题
- javascript - 如何在反应中将同步数据从功能发送到基于类的组件?