0

Given an array of objects arr1 how can I filter out to a new array the objects that do not have a property equal to any value in the array of numbers arr2

const arr1 = [
  {
    key: 1,
    name: 'Al'
  },
  {
    key: 2,
    name: 'Lo'
  },
  {
    key: 3,
    name: 'Ye'
  }
];

const arr2 = [2, 3]

// Failed attempt
const newArr = arr1.filter(obj1 => arr2.some(num1 => num1 !== obj1.key))
console.log(newArr)

// Expected: [{ key: 1, name: 'Al' }]

// Received: [
//   { key: 1, name: 'Al' },
//   { key: 2, name: 'Lo' },
//   { key: 3, name: 'Ye' }
// ]
c_lejeune
  • 215
  • 2
  • 6
  • Does this answer your question? [filtering an array of objects based on another array in javascript](https://stackoverflow.com/questions/46894352/filtering-an-array-of-objects-based-on-another-array-in-javascript) – Heretic Monkey Jan 30 '20 at 19:48

6 Answers6

4

Using your syntax:

You have to match on the somein case it's the same and not different. Then if it matches, do not keep the value.

const arr1 = [
  {
    key: 1,
    name: 'Al',
  },
  {
    key: 2,
    name: 'Lo',
  },
  {
    key: 3,
    name: 'Ye',
  },
];

const arr2 = [2, 3];

const newArr= arr1.filter(x => !arr2.some(y => y === x.key));

console.log(newArr);

Alternative syntax below :

const arr1 = [{
    key: 1,
    name: 'Al',
  },
  {
    key: 2,
    name: 'Lo',
  },
  {
    key: 3,
    name: 'Ye',
  },
];

const arr2 = [2, 3];

const newArr = arr1.filter(({
  key,
}) => !arr2.some(y => y === key));

console.log(newArr);

That said, you should be using Array.includes() like some ppl answered. It's simplier for the same outcome

const arr1 = [{
    key: 1,
    name: 'Al',
  },
  {
    key: 2,
    name: 'Lo',
  },
  {
    key: 3,
    name: 'Ye',
  },
];

const arr2 = [2, 3];

const newArr = arr1.filter(({
  key,
}) => !arr2.includes(key));

console.log(newArr);
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
2

You can do this

const newArr = arr1.filter(obj => !arr2.includes(obj.key));
Abito Prakash
  • 4,368
  • 2
  • 13
  • 26
2

This will work for you:

const arr1 = [
  {
    key: 1,
    name: 'Al'
  },
  {
    key: 2,
    name: 'Lo'
  },
  {
    key: 3,
    name: 'Ye'
  }
];

const arr2 = [2, 3]

const filtered = arr1.filter(val => !arr2.includes(val.key))

console.log(filtered)

:)

Max
  • 1,996
  • 1
  • 10
  • 16
1

For situations like this Set is also very cool (and for big arrays more performant):

const arr1 = [
  {
    key: 1,
    name: 'Al'
  },
  {
    key: 2,
    name: 'Lo'
  },
  {
    key: 3,
    name: 'Ye'
  }
];

const arr2 = [2, 3]
const arr2Set = new Set(arr2);

const newArr = arr1.filter(obj1 => !arr2Set.has(obj1.key))
console.log(newArr)
Kaca992
  • 2,211
  • 10
  • 14
0

You can use indexOf like this:

const newArr = arr1.filter(obj => arr2.indexOf(obj.key) > -1);
technophyle
  • 7,972
  • 6
  • 29
  • 50
0

You need to filter the arr1 when arr1 element does not exist in arr2, so I think it could be better to use indexOf() like this

const newArr = arr1.filter(obj1 => arr2.indexOf(obj1.key) === -1)

if the element does not exist in arr2 it will return -1 which what you need.

Ahmad Kayali
  • 34
  • 1
  • 3