如何仅从2个数组中返回非重复对象

时间:2019-02-25 15:10:51

标签: javascript arrays

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

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

谢谢!

4 个答案:

答案 0 :(得分:2)

我将首先连接两个数组,然后使用filter()搜索两个数组中未出现在两个数组中的那些元素。请注意,这种方法考虑了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);

答案 1 :(得分:1)

您可以通过迭代firstArray来过滤secondArray并检查所需属性以进行补偿。

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);

答案 2 :(得分:1)

您可以过滤一个数组,然后将对象与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);

答案 3 :(得分:0)

另一种方法可以基于.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);