Simplest code for array intersection in javascript
问题 What\'s the simplest, library-free code for implementing array intersections in javascript? I want to write intersection([1,2,3], [2,3,4,5]) and get [2, 3] 回答1: Use a combination of Array.prototype.filter and Array.prototype.indexOf: array1.filter(value => -1 !== array2.indexOf(value)) Or as vrugtehagel suggested in the comments, you can use the more recent Array.prototype.includes for even simpler code: array1.filter(value => array2.includes(value)) For older browsers: array1.filter(function