如何删除两个javascript数组之间的重复元素?

时间:2016-08-10 05:45:45

标签: javascript arrays

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] }];

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

这意味着:

我想得到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

4 个答案:

答案 0 :(得分:2)

您可以使用.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);

答案 1 :(得分: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
}

答案 2 :(得分:0)

首先:你的问题对我来说有点不清楚。如果你澄清一下,我可以更好地回答这个问题。我想你正试图从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
    }
}

答案 3 :(得分:0)

为了确保正确比较所有元素,您应该按id对它们进行排序,以确保将正确的数组元素进行比较。此解决方案假定对象只有另一个要比较的属性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