1

I'm trying to get information, a have all the data that i get from Database and now I need to do something that should be simple (but not for a novice like me xd), I'm trying to do the next filter in JS

const array1 = [{id: 'a'}, {id: '8'}, {id: 'c'}, {id: 'a'}];
const array2 = [{id: 'a'}, {id: 'c'}];
console.log(array1.filter(id => id.id == array2.id))

It doesn't returns me nothing, and I don't understand why, also I try to use:

console.log(array1.filter(id => id.id == array2.id))

My result is all the values from array1, I mean I only want the elements that have the same Id that array2 from array1, in other words, I want that its returns me 2 objects with Id = a and one with id = c

wangdev87
  • 8,611
  • 3
  • 8
  • 31
user14860979
  • 107
  • 1
  • 2
  • 11
  • `It doesn't returns me nothing` - so it returns everything? ;) – Jamiec Feb 09 '21 at 18:25
  • If you want a somewhat non-verbose way of writing this I would, instead of native filter+some use lodashs intersectionBy function. _.intersectionBy(array1, array2, 'id'); It's a pretty neat way to get the intended results. – theodore hogberg Feb 09 '21 at 18:36

4 Answers4

2

Use Array.some() inside Array.filter() callback method.

const array1 = [{id: 'a'}, {id: '8'}, {id: 'c'}, {id: 'a'}];
const array2 = [{id: 'a'}, {id: 'c'}];
const output = array1.filter(item1 => array2.some(item2 => item2.id === item1.id))
console.log(output);
wangdev87
  • 8,611
  • 3
  • 8
  • 31
1

This will return three objects, because in array1 there are two objects with id: a and one with id: c.

const array1 = [{id: 'a'}, {id: '8'}, {id: 'c'}, {id: 'a'}];
const array2 = [{id: 'a'}, {id: 'c'}];
let res = array1.filter(obj1 => array2.find(obj2 => obj1.id === obj2.id))

console.log(res)

You can filter out duplicate objects with Array.reduce():

const array1 = [{id: 'a'}, {id: '8'}, {id: 'c'}, {id: 'a'}];
const array2 = [{id: 'a'}, {id: 'c'}];
let res = array1.filter(obj1 => {
  return array2.find(obj2 => obj1.id === obj2.id)
})
.reduce((acc,cur) => {
  if(!acc.find(obj => obj.id === cur.id)){
    acc.push(cur)
  }
  return acc
},[])

console.log(res)
symlink
  • 11,984
  • 7
  • 29
  • 50
0

You can to use .some to check if array2 has an element with a given id:

const array1 = [{id: 'a'}, {id: '8'}, {id: 'c'}, {id: 'a'}];
const array2 = [{id: 'a'}, {id: 'c'}];

const res = array1.filter(({id}) => array2.some(e => e.id===id));

console.log(res);

A better way would be using a Set:

const array1 = [{id: 'a'}, {id: '8'}, {id: 'c'}, {id: 'a'}];
const array2 = [{id: 'a'}, {id: 'c'}];

const idsInArray2 = new Set(array2.map(({id}) => id));
const res = array1.filter(({id}) => idsInArray2.has(id));

console.log(res);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

You could take a Set and filter the array.

This approach takes a single loop for building the set and another for filtering.

const
    array1 = [{ id: 'a' }, { id: '8' }, { id: 'c' }, { id: 'a' }],
    array2 = [{ id: 'a' }, { id: 'c' }],
    set2 = new Set(array2.map(({ id }) => id)),
    result = array1.filter(({ id }) => set2.has(id));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392