如何删除两个javascript数组之间的重复元素? a=[ {id:1,书籍:[1,2,3,4]}, {id:2,书籍:[1,2,3]}, {id:3,书籍:[1,2,3,4,5]}, {id:9,书籍:[1,2]} ]; b=[{id:2,图书:[1,2,3]},{id:3,图书:[1,2,3,4,5]}];

如何删除两个javascript数组之间的重复元素? a=[ {id:1,书籍:[1,2,3,4]}, {id:2,书籍:[1,2,3]}, {id:3,书籍:[1,2,3,4,5]}, {id:9,书籍:[1,2]} ]; b=[{id:2,图书:[1,2,3]},{id:3,图书:[1,2,3,4,5]}];,javascript,arrays,Javascript,Arrays,我想从a中删除数组b相同的id元素。 怎么做?谢谢 这意味着: 我想得到a=[{id:1,books:[1,2,3,4]}],去掉数组b中的相同元素 我的代码是: constdelduplicate=(a,b)=>{ 常数bLen=b.长度; 如果(!bLen)返回a; for(设i=0;i […res]=[…res.filter(Boolean),ids.indexOf(o.id)o.id); a=a.reduce((res,o)=> […res]=[…res.filter(Boolean),

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

这意味着:

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

我的代码是:

constdelduplicate=(a,b)=>{
常数bLen=b.长度;
如果(!bLen)返回a;
for(设i=0;i
这很有效,有更好的方法吗?我认为
reduce
map
可能也有用


这两个数组不是简单的数组。所以不能使用
a.indexOf(b[i])!=-首先:你的问题我有点不清楚。如果你澄清,我可以把这个答案做得更好。我认为您正在尝试从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,图书:[1,2,3]},{id:3,图书:[1,2,3,4,5]}]
//将循环大小设置为较小数组的大小
var lsize=b.长度;

if(a.length首先:我对你的问题有点不清楚。如果你澄清,我可以更好地回答这个问题。我估计你正试图从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,图书:[1,2,3]},{id:3,图书:[1,2,3,4,5]}]
//将循环大小设置为较小数组的大小
var lsize=b.长度;
如果(a.length,您可以使用
.map()
.reduce()
.filter()
.indexOf()

varids=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-id=b.map(o=>o.id);
a=a.reduce((res,o)=>
[…res]=[…res.filter(Boolean),ids.indexOf(o.id)<0&&o],];
console.log(a);
您可以使用
.map()
.reduce()
.filter()
.indexOf()

varids=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-id=b.map(o=>o.id);
a=a.reduce((res,o)=>
[…res]=[…res.filter(Boolean),ids.indexOf(o.id)<0&&o],];

console.log(a);
要确保正确比较所有元素,应按
id
s对它们进行排序,以确保将正确的数组元素比较在一起。此解决方案假定对象只有另一个要比较的属性,
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
//初始化数组
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 comp=功能(a,b){
返回a.id-b.id;
};
//数组a的索引
var j=0;
//排序数组
a、 排序(comp);
b、 排序(comp);
//对照a中具有匹配ID的元素检查b中的所有元素

对于(var i=0;i要确保正确比较所有元素,应按
id
s对它们进行排序,以确保将正确的数组元素比较在一起。此解决方案假定对象只有另一个要比较的属性,
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
//初始化数组
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 comp=功能(a,b){
返回a.id-b.id;
};
//数组a的索引
var j=0;
//排序数组
a、 排序(comp);
b、 排序(comp);
//对照a中具有匹配ID的元素检查b中的所有元素

对于(var i=0;i我找到了另一种方法。通过在两个数组中循环,如果没有找到匹配/重复项,则将此元素添加到第三个数组中。如果需要,第三个数组可以在末尾重写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
}
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=[];//对解析的数组进行排序的新数组
var boolMatch;//如果发现重复项,则为布尔值
对于(i=0;ivar 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
}