简体   繁体   English

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

[英]Remove values in nested array of objects based on values from another array

I have an array of objects with the following structure:我有一个具有以下结构的对象数组:

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

I also have another array called imagesToDelete with the following values:我还有另一个名为 imagesToDelete 的数组,其值如下:

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

My goal is to remove values from the nested array based on the values in the imagesToDelete array.我的目标是根据imagesToDelete数组中的嵌套数组中删除 If done correctly, this will be the following result:如果操作正确,这将是以下结果:

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

Below is my current code which did not remove any values:下面是我当前的代码,它没有删除任何值:

 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)

There are a couple of issues.有几个问题。 Mostly, you never actually modify the images array in your iterations, and filter based on the optionList which isn't actually removing any items (since if there are no images you state it is left as an empty array).大多数情况下,您从未在迭代中实际修改图像数组,并根据实际上并未删除任何项目的 optionList 进行过滤(因为如果您声明没有图像,它将保留为空数组)。

You need to iterate optionList, and modify the images array based on the list for deletion.需要迭代optionList,根据list修改images数组进行删除。

You can use forEach for the outer iteration.您可以将forEach用于外部迭代。

 let optionList = [ { images: [ { url: "test1" }, { url: "test2" } ] }, { images: [ { url: "test3" }, { url: "test4" } ] }, { images: [ { url: "test5" }, { url: "test6" } ] } ]; let imagesToDelete = ["test1", "test3", "test6"]; optionList.forEach(ol => ol.images = ol.images.filter( image => !imagesToDelete.includes(image.url) ) ); console.log(optionList);

filter() returns a new array and does not mutate original filter()返回一个新数组并且不会改变原始数组

Try doing尝试做

 const newList = optionList.filter(ol => { let result = !ol.images.some( image => image.url === imagesToDelete.includes(image.url) ); return result; }); console.log(newList);
 <script> let imagesToDelete = ["test1", "test3", "test6"]; let optionList = [{images: [{url: "test1"},{url: "test2"}]},{images: [{url: "test3"},{url: "test4"}]},{images: [{url: "test5"},{url: "test6"}]}] </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个数组中的值从数组中删除对象 - Remove objects from an Array based on values in another array 从基于另一个数组的对象数组中删除不相似的值 - remove non similar values from an array of objects based on another array 从嵌套在另一个对象数组中的对象数组中访问值 - accessing values from an array of objects nested within another array of objects 根据来自另一个对象数组的多个值过滤对象数组 - Filter array of objects based on multiple values from another array of objects 通过匹配值从我的嵌套对象数组中删除数据 - Remove data from my nested array of objects by matching values 检查对象数组是否包含来自另一个对象数组的多个值并删除重复项 - Check if array of objects contains multiple values from another array of objects and remove duplicates 如何根据嵌套数组中的值过滤带有对象的数组并对其进行计数 - How to filter array with objects based on values in nested array and counting them 从 JavaScript 中的嵌套对象数组中提取值 - Extracting values from Array of nested Objects in JavaScript 根据包含来自另一个数组的所有值的数组属性过滤对象数组 - Filter an array of objects based on array property containing all values from another array 从javascript数组中删除嵌套的null值 - remove nested null values from javascript array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM