【问题标题】:How to delete duplicate elements between two javascript arrays?如何删除两个javascript数组之间的重复元素?
【发布时间】:2019-02-10 12:36:28
【问题描述】:
a = [
  { id: 1, books: [1, 2, 3, 4] },
  { id: 2, books: [1, 2, 3] },
  { id: 3, books: [1, 2, 3, 4, 5] },
  { id: 9, books: [1, 2] }
];

b = [{ id: 2, books: [1, 2, 3] }, { id: 3, books: [1, 2, 3, 4, 5] }];

我想从a 中删除数组b 相同的id 元素。 怎么做?谢谢。

意思是:

我想得到a = [{id: 1, books: [1,2,3,4]}],去掉数组b中的相同元素;

我的代码是:

const delDuplicate = (a, b) => {
  const bLen = b.length;
  if (!bLen) return a;
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < bLen; j++) {
      if (a[i].id === b[j].id) {
        const delItem = a.splice(i, 1)[0];
        console.log(delItem);
      }
    }
  }
  return a;
};

a = delDuplicate(a, b);

有效,有没有更好的方法?我认为reducemap 也可以。

这两个数组不是简单的数组。所以不能用a.indexOf(b[i]) !== -1

【问题讨论】:

  • 你自己有没有尝试过?
  • 您能否澄清一下:您想从第二个数组中删除两个数组中匹配的元素(相同的位置和值)?还是您想从第二个数组中删除具有相同 id 的元素,而不管 value/pos 是什么?
  • @AshwinGupta 我编辑了问题,对不起,我的英语不好。

标签: javascript arrays


【解决方案1】:

您可以使用.map().reduce().filter().indexOf()

var ids = b.map(o => o.id);
a = a.reduce((res, o) => 
      [...res] = [...res.filter(Boolean), ids.indexOf(o.id) < 0 && o], []);

var a = [{id: 1, books: [1,2,3,4]}, {id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}, {id: 9, books: [1,2]}];
var b = [{id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}];

var ids = b.map(o => o.id);
    a = a.reduce((res, o) => 
      [...res] = [...res.filter(Boolean), ids.indexOf(o.id) < 0 && o], []);

console.log(a);

【讨论】:

    【解决方案2】:

    我找到了另一种方法来做到这一点。通过循环遍历两个数组,如果未找到匹配/重复,则将此元素添加到第三个数组。如果需要,第三个数组可以覆盖最后的 A 数组。已经过测试和工作。

    var a = [{id: 1, books: [1,2,3,4]}, {id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}, {id: 9, books: [1,2]}];
    var b = [{id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}];
    var c = []; // New array to sort parsed A array
    var boolMatch; // Boolean if duplicate is found
    
    for(i = 0; i < a.length; i++){
        boolMatch = false; // Required to reset the Boolean at the start of each loop
        for(j = 0; j < b.length; j++){
            if(a[i].id == b[j].id){
                boolMatch = true;
                break;
            }
        }
        if(!boolMatch) c.push(a[i]); // Add to C array if element from A is NOT found in B array
    }
    

    【讨论】:

      【解决方案3】:

      首先:你的问题对我来说有点不清楚。如果您澄清,我可以使这个答案更好。我假设您正在尝试从 b 中删除与数组 a 中的相应元素具有相同值的元素。我还假设您想在搜索期间更新发生率。

      现在 IDK 是 Javascript 的确切语法,所以这可能有点偏离。但是,它应该让您大致了解该做什么。 (我会在研究一下后尝试修复代码)

      a = [{id: 1, books: [1,2,3,4]}, {id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}, {id: 9, books: [1,2]}]
      b = [{id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}]
      
      //set loop size to size of the smaller array
      var lsize=b.length;
      if(a.length<b.length) lsize = a.length;
      
      //loop through length 
      for(var i = 0; i < lsize; i++) {
          if(a[i] != b[i]) { //check if the values of the elements are the same
             b.splice(i, 1); //remove the element from b
             i=-1; //reset loop to check rest of elements
             lsize-=1; //reduce size since there is one less
          }
      }
      

      【讨论】:

        【解决方案4】:

        为确保正确比较所有元素,应按ids 对它们进行排序,以确保将正确的数组元素一起比较。此解决方案假定对象只有一个要比较的其他属性,books,它是一个排序数组。

        // initialize arrays
        var a = [{id: 1, books: [1,2,3,4]}, {id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}, {id: 9, books: [1,2]}];
        var b = [{id: 2, books: [1,2,3]}, {id: 3, books: [1,2,3,4,5]}];
        // comparator
        var comp = function(a,b) {
            return a.id-b.id;
        };
        // index for array a
        var j = 0;
        // sort arrays
        a.sort(comp);
        b.sort(comp);
        
        // check all elements in b against those with matching ids in a
        for(var i=0; i<b.length; i++) {
            // find next matching id in a
            while(a[j].id<b[i]&&j<a.length) {
                j++;
            }
            // break if no more elements to check against in a
            if(j==a.length) {
                break;
            }
        
            // compare elements with matching ids to see if the books array make the same string
            // comparing array references won't work, so they're converted to strings instead
            if(a[j].id==b[i].id&&a[j].books.join(",")==b[i].books.join(",")) {
                // remove element from b and don't skip over next element
                b.splice(i,1);
                i--;
            }
        }
        // b should share no elements with a
        

        【讨论】:

          猜你喜欢
          • 2014-09-27
          • 2019-02-11
          • 1970-01-01
          • 2019-11-13
          • 1970-01-01
          • 2016-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多