locutusjs/locutus

[php/array/array_intersect.js] Simpler option

geoidesic opened this issue · 3 comments

  • Have you checked the guidelines in our Contributing document?

Description

Wouldn't this be simpler?

export const intersection = (arr1, arr2) => {
  return arr1.filter(x => arr2.includes(x));
};

The problem with this is that this only compares array two with array one. If you look at the implementation, array_intersect() can take can take multiple arrays, and even objects, then compare all of them.

//   example 1: var $array1 = {'a' : 'green', 0:'red', 1: 'blue'}
//   example 1: var $array2 = {'b' : 'green', 0:'yellow', 1:'red'}
//   example 1: var $array3 = ['green', 'red']
//   example 1: var $result = array_intersect($array1, $array2, $array3)
//   returns 1: {0: 'red', a: 'green'}