intersection.js 574 B

12345678910111213141516171819
  1. import getLength from './_getLength.js';
  2. import contains from './contains.js';
  3. // Produce an array that contains every item shared between all the
  4. // passed-in arrays.
  5. export default function intersection(array) {
  6. var result = [];
  7. var argsLength = arguments.length;
  8. for (var i = 0, length = getLength(array); i < length; i++) {
  9. var item = array[i];
  10. if (contains(result, item)) continue;
  11. var j;
  12. for (j = 1; j < argsLength; j++) {
  13. if (!contains(arguments[j], item)) break;
  14. }
  15. if (j === argsLength) result.push(item);
  16. }
  17. return result;
  18. }