0

I need to return the element of first array NOT present in the second, return empty array if both are present in second array

arr1 = [' name', 'email']

arr2 = [ 'name', 'card_number', 'role', 'age' ]

expect to return ['email'] in this example

tried to use filter but didn't succeed.

anyone can help, please?

John
  • 13
  • 1

1 Answers1

3

You could look if the other array does not includ the value for filtering.

const
    arr1 = ['name', 'email'],
    arr2 = ['name', 'card_number', 'role', 'age'],
    result = arr1.filter(v => !arr2.includes(v));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392