比较两个数组并按对象ID删除对象

时间:2018-12-20 10:26:32

标签: javascript arrays json object

我尝试过的代码是:

    const data1 = [{ id: 1, name: 'dd' }, { id: 2, name: 'dd' }, { id: 3, name: 'dd' }, { id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
    const sasas = [];
    const data2 = [{ id: 2, name: 'dd' },{ id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
    data2.map((io) => {
     sasas.push(io.id);
     });
    
    sasas.forEach((ik) => {
     for (let i = 0; i < data1.length - 1; i++) {
       if (data1[i].id === ik) {
          data1.splice(i, 1);
            }
          }
      });
    console.log(data1);

我有两个数组,我映射了 data2 数据并将ID值获取到 sasas

然后针对每个 sasas 数据,

并使用forloop 拼接 data1 的索引数据。

然后得到答案是 data1 中的id:1,id:3,id:5 对象

但是我预期的输出是:

const data1 = [{ id: 1, name: 'dd' },{ id: 3, name: 'dd' }];

请帮助我!

4 个答案:

答案 0 :(得分:3)

尝试

data1.filter(x=> !data2.some(y=> y.id==x.id ))

const data1 = [{ id: 1, name: 'dd' }, { id: 2, name: 'dd' }, { id: 3, name: 'dd' }, { id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
const data2 = [{ id: 2, name: 'dd' },{ id: 4, name: 'dd' }, { id: 5, name: 'dd' }];

let result = data1.filter(x=>!data2.some(y=>y.id==x.id))

console.log(result);

较短的求解时间复杂度为 O(n * m),低于 O(n + m)版本(n,m为数组长度):

let h = {}; data2.map(x=>h[x.id]=1); 
let result = data1.filter(x=>!h[x.id]);

const data1 = [{ id: 1, name: 'dd' }, { id: 2, name: 'dd' }, { id: 3, name: 'dd' }, { id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
const data2 = [{ id: 2, name: 'dd' },{ id: 4, name: 'dd' }, { id: 5, name: 'dd' }];

let h = {}; data2.map(x=>h[x.id]=1);     // we use hash map with data2 ids
let result = data1.filter(x=>!h[x.id]);

console.log(result);

答案 1 :(得分:2)

您可以尝试以下两种方法。另外,可以使用Set(更好的性能)代替创建数组

使用while循环更新现有阵列

const data1 = [{ id: 1, name: 'dd' }, { id: 2, name: 'dd' }, { id: 3, name: 'dd' }, { id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
const data2 = [{ id: 2, name: 'dd' },{ id: 4, name: 'dd' }, { id: 5, name: 'dd' }];

const sasas = data2.reduce((a,{id}) => a.add(id), new Set())
let i = 0;
while(i < data1.length) {
  if(sasas.has(data1[i].id)) {
    data1.splice(i,1);
  } else i++;
}
console.log(data1);

使用Array.filter创建新数组

const data1 = [{ id: 1, name: 'dd' }, { id: 2, name: 'dd' }, { id: 3, name: 'dd' }, { id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
const data2 = [{ id: 2, name: 'dd' },{ id: 4, name: 'dd' }, { id: 5, name: 'dd' }];

const sasas = data2.reduce((a,{id}) => a.add(id), new Set())

const result = data1.filter(({id}) => !sasas.has(id));
console.log(result);

答案 2 :(得分:2)

首先,您可以更好地使用该地图,而不是

data2.map((io) => {
    sasas.push(io.id);
});

您可以简单地:

sasas = data2.map(io => io.id);

您的错误:

for (let i = 0; i < data1.length - 1; i++) {

这意味着您无需检查数组的最后一个索引,可以将<更改为<=,也可以直接删除-1

这应该有效:

for (let i = 0; i < data1.length; i++) {

Array.filter

您想要实现的功能可以通过.filter()函数轻松获得,如下所示:

    const data1 = [{ id: 1, name: 'dd' }, { id: 2, name: 'dd' }, { id: 3, name: 'dd' }, { id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
    const data2 = [{ id: 2, name: 'dd' },{ id: 4, name: 'dd' }, { id: 5, name: 'dd' }];
    const sasas = data2.map((io) => io.id);
    
    var result = data1.filter(e => sasas.indexOf(e.id) === -1)
    console.log(result);

答案 3 :(得分:1)

由于此循环,我停止在data1.length-2

for (let i = 0; i < data1.length - 1; i++) {

变量i应该从0到data1.length-1,这使得条件变为

i < data1.length