0

I'm trying to intersect array1 and array2 and find the elements that contain the same name.

Then on array3 I only want to keep the elements that exist on the first intersection by name.

I'm stuck here, I just get true and falses. Any help?

const array1 = [{
  name: 'John'
}];
const array2 = [{
  name: 'Elisa'
}, {
  name: 'John'
}];

const array3 = [{
  name: 'Elisa',
  age: 10
}, {
  name: 'John',
  age: 23
}, {
  name: 'Maria',
  age: 30
}];

const intersectArray = array1.map(elem1 => array2.map(elem2 => elem1.name === elem2.name));

console.log(intersectArray);

const filteredArray = array3.map(elem3 => intersectArray.map(elem => elem.name === elem3.name));

console.log(filteredArray);

The expected result should be:

{ name: 'John', age: 23 }

Tiago Silva
  • 229
  • 2
  • 9
  • 1
    Does this answer your question? [Difference and intersection of two arrays containing objects](https://stackoverflow.com/questions/33356504/difference-and-intersection-of-two-arrays-containing-objects) – Heretic Monkey Oct 26 '22 at 12:42

3 Answers3

1

You can just check against both arrays, instead of first creating an intersection:

const array1 = [{ name: 'John' }];
const array2 = [{ name: 'Elisa' }, { name: 'John' }];
const array3 = [{ name: 'Elisa', age: 10 }, { name: 'John', age: 23 }, { name: 'Maria', age: 30 }];

const result = array3.filter(x => 
    array1.some(a => a.name === x.name) &&
    array2.some(a => a.name === x.name))
console.log(result);
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

To do what you require you can use filter() to return only the elements which meet certain criteria. In your case, you can find() within the other array if a matching name is found.

The same logic can be used for both steps:

const array1 = [{ name: 'John' }];
const array2 = [{ name: 'Elisa' }, { name: 'John' }];
const array3 = [{ name: 'Elisa', age: 10 }, { name: 'John', age: 23 }, { name: 'Maria', age: 30 }];

const intersectArray = array1.filter(o1 => array2.find(o2 => o1.name == o2.name));
console.log(intersectArray);

const filteredArray = array3.filter(o3 => intersectArray.find(ia => o3.name === ia.name));
console.log(filteredArray);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

If you want to grab the intersection between three arrays, you can filter the first array and find a similar item in the second array with some.

const
  intersection = (a, b, f) => a.filter(x => b.some(y => f(x) === f(y))),
  intersection2 = (f, ...a) => a.reduce((r, b) => intersection(r, b, f));
  
const
  arr1 = [{ name: 'John' }],
  arr2 = [{ name: 'Elisa' }, { name: 'John' }],
  arr3 = [{ name: 'Elisa', age: 10 }, { name: 'John', age: 23 }, { name: 'Maria', age: 30 }];

const
  getName = ({ name }) => name,
  inter1 = intersection(arr3, intersection(arr2, arr1, getName), getName),
  inter2 = intersection2(getName, arr3, arr2, arr1);

console.log(inter1); // Nested calls
console.log(inter2); // Reduction
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132