_.intersection

Returns an array that is the intersection of all the arrays. Each value in the result is present in each of the arrays.

  1. // Underscore/Lodash
  2. console.log(_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]))
  3. // output: [1, 2]
  4.  
  5. // Native
  6. var arrays = [[1, 2, 3], [101, 2, 1, 10], [2, 1]];
  7. console.log(array.reduce(function(a, b) {
  8. return a.filter(function(value) {
  9. return b.includes(value);
  10. });
  11. })));
  12. // output: [1, 2]
  13.  
  14. // ES6
  15. let arrays = [[1, 2, 3], [101, 2, 1, 10], [2, 1]];
  16. console.log(arrays.reduce((a, b) => a.filter(c => b.includes(c))));
  17. // output: [1, 2]

Browser Support for Array.prototype.reduce()

ChromeEdgeFirefoxIEOperaSafari
3.0 ✔9.0 ✔10.5 ✔4.0 ✔