【问题标题】:How to return only non-duplicated objects from 2 arrays如何从 2 个数组中仅返回不重复的对象
【发布时间】:2025-12-15 01:05:01
【问题描述】:

我发现了很多类似的问题,但它们解释了如何删除重复的对象。在这种情况下,我需要创建一个不包含在两个数组中找到的对象的新数组。

const firstArray = [
  {firstName: 'John', lastName: 'Doe'}, 
  {firstName: 'Sara', lastName: 'Connor'},
  {firstName: 'Mike', lastName: 'Hunt'}, 
  {firstName: 'Steve', lastName: 'Irvine'}
];

const secondArray = [ 
  {firstName: 'John', lastName: 'Doe'},  
  {firstName: 'Sara', lastName: 'Connor'} 
];

上一个数据样本的预期结果应该是:

const result = [
  {firstName: 'Mike', lastName: 'Hunt'},
  {firstName: 'Steve', lastName: 'Irvine'}
];

【问题讨论】:

标签: javascript arrays


【解决方案1】:

我将首先连接两个数组,然后 filter() 使用 some() 对每个数组进行搜索的那些没有出现在两个数组上的元素。请注意,此方法考虑了firstArraysecondArray 都可以包含非重复对象的可能性。

const firstArray = [
  {firstName: 'John', lastName: 'Doe'}, 
  {firstName: 'Sara', lastName: 'Connor'},
  {firstName: 'Mike', lastName: 'Hunt'}, 
  {firstName: 'Steve', lastName: 'Irvine'}
];

const secondArray = [ 
  {firstName: 'John', lastName: 'Doe'},  
  {firstName: 'Sara', lastName: 'Connor'},
  {firstName: 'Joseph', lastName: 'Muguera'}
];

let res = firstArray.concat(secondArray).filter(({firstName, lastName}) =>
{
    let foundOnFirst = firstArray.some(
        x => x.firstName === firstName && x.lastName === lastName
    );
    let foundOnSecond = secondArray.some(
        y => y.firstName === firstName && y.lastName === lastName
    );
    return !(foundOnFirst && foundOnSecond);
});

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

【讨论】:

    【解决方案2】:

    您可以通过迭代 secondArray 来过滤 firstArray 并检查想要的属性以进行比较。

    const
        firstArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }, { firstName: 'Mike', lastName: 'Hunt' }, { firstName: 'Steve', lastName: 'Irvine' }],
        secondArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }],
        result = firstArray.filter(o => 
            secondArray.every(p =>
                !['firstName', 'lastName'].some(k => o[k] === p[k])));
    
    console.log(result);

    一种具有Set 的联合属性的方法。

    const
        firstArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }, { firstName: 'Mike', lastName: 'Hunt' }, { firstName: 'Steve', lastName: 'Irvine' }],
        secondArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }],
        getKey = ({ firstName, lastName }) => [firstName, lastName].join('|'),
        secondSet = new Set(firstArray.map(getKey)),
        result = firstArray.filter(o => !set.has(getKey(o)));
    
    console.log(result);

    【讨论】:

    • 多么简洁的解决方案!谢谢!
    【解决方案3】:

    您可以过滤一个数组并将对象与JSON.stringify 与第二个数组进行比较,

    const firstArray = [
        {firstName: 'John', lastName: 'Doe'}, 
        {firstName: 'Sara', lastName: 'Connor'},
        {firstName: 'Mike', lastName: 'Hunt'}, 
        {firstName: 'Steve', lastName: 'Irvine'}
        ];
    
    const secondArray = [ 
        {firstName: 'John', lastName: 'Doe'},  
        {firstName: 'Sara', lastName: 'Connor'} 
        ];
        
    const res=firstArray.filter(function(obj) {
     // JSON.stringify(obj)==JSON.stringify(obj)
      for( var i=0, len=secondArray.length; i<len; i++ ){
              if(JSON.stringify(obj)==JSON.stringify(secondArray[i]) ) {
                  return false;
              }
          }
    	return true; 
    });
    console.log(res);

    【讨论】:

      【解决方案4】:

      另一种方法可以基于.findIndex()JSON.stringify()

      const firstArray = [
          {firstName: 'John', lastName: 'Doe'},
          {firstName: 'Sara', lastName: 'Connor'},
          {firstName: 'Mike', lastName: 'Hunt'},
          {firstName: 'Steve', lastName: 'Irvine'}
      ];
      
      const secondArray = [
          {firstName: 'John', lastName: 'Doe'},
          {firstName: 'Sara', lastName: 'Connor'}
      ];
      const result = firstArray.filter(ele =>
          secondArray.findIndex(ele1 =>
              JSON.stringify(ele1) == JSON.stringify(ele)) == -1
          );
      
      console.log(result);

      【讨论】: