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' }
// ]
1

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can do this

const newArr = arr1.filter(obj => !arr2.includes(obj.key));

Comments

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)

Comments

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)

Comments

0

You can use indexOf like this:

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.