【问题标题】:Removing items from an array based on another array基于另一个数组从数组中删除项目
【发布时间】:2018-07-18 02:05:42
【问题描述】:

我正在开发一个数据处理程序,并且快完成了。我已经使重复检测算法正常工作,并且我有一个包括重复项在内的所有项目的列表和一个重复项列表。

我想浏览项目列表,按重复项列表过滤,并删除除第一个重复项之外的所有项。我试过这样,但它实际上并没有从数组中删除记录。

const removeDupes = (list, dupes) => {
  list.forEach(listItem => {
    let filtered = dupes.filter(x => ((x.item1.externalId === listItem.externalId)|| (x.item2.externalId === listItem.externalId)));
    if(filtered.length > 0){
      for(let i = 1; i < filtered.length; i++){
        list.splice(list.indexOf(filtered[i]));
      }
    }
  });
  return list;
}

请记住,listdupes 的架构略有不同。 list 只是一个对象数组,其 ID 字段名为 externalIDdupes 是一个具有此架构的对象数组:

[{
    item1: {schema from list},
    item2: {schema from list},
...}]

它们不是完全重复的,更像是来自具有不同架构的不同数据库的副本,这些数据库已被重新格式化为相同的架构......

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    我会这样做:

    const noDuplicates = [...(new Set(duplicates))]; // This creates an array that removes duplicated items in it
    

    然后将它与您的原始列表进行比较:

    const removeDupes = list.filter(value => !noDuplicates.includes(value)); // This will add the value if it is not in the list
    

    让我知道这是否适合你

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 2016-07-03
      • 2012-04-18
      • 2018-10-19
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多