So, I wanted the elements of the array to arr1also belong to the array arr2. I decided that I arr1.filter(arr2.includes)should do the trick, but he gave me an error (see below). Strange, however, arr1.filter(x => arr2.incudes(x))worked fine. Even if the functions arr2.includesand x => arr2.includes(x)are not reference, should not they take the same value on the same inputs? What am I missing here?
> arr1 = ['a', 'b', 'c']
[ 'a', 'b', 'c' ]
> arr2 = ['a', 'c', 'd']
[ 'a', 'c', 'd' ]
>
> arr1.filter(x => arr2.includes(x))
[ 'a', 'c' ]
> arr1.filter(arr2.includes)
TypeError: Cannot convert undefined or null to object
at includes (<anonymous>)
at Array.filter (native)
at repl:1:6
... etc ...
source
share