0

Insted of doing using an anonymous function how would I convert the below element => arr2.includes(element) into a named function that I could then pass in

  const ArrayOverlap = (arr1, arr2) =>{
  let newArr = [];
  return newArr = arr1.filter(element => arr2.includes(element));
}

If I was to do something like

  const ArrayOverlap = (arr1, arr2) =>{
  let newArr = [];
  return newArr = arr1.filter(bothIncluded(arr1, arr2));
}

 function bothIncluded(arr1, arr2){
  for(const item of arr1){
   return arr2.includes(item);
}
Aychembee
  • 1
  • 2
  • Sorry--what is the question here? You want the [intersection](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) of the arrays? – ggorlen Jul 11 '22 at 23:56
  • Yeah I want to find what elements overlap, but instead of using an anonymous function like element => arr2.includes(element) in the filter, I want to use a named function and pass the two arrays? or how would I go converting element => arr2.includes(element) into a named function – Aychembee Jul 11 '22 at 23:58

1 Answers1

0

I was able to get it to work by using the option thisArg when calling the callback function.

const ArrayOverlap = (arr1, arr2) =>{
let newArr = [];
return newArr = arr1.filter(checkMe, arr2);

function checkMe(element){
return this.includes(element);
}
Aychembee
  • 1
  • 2