intersection.js 599 B

123456789101112131415161718192021
  1. var _getLength = require('./_getLength.js');
  2. var contains = require('./contains.js');
  3. // Produce an array that contains every item shared between all the
  4. // passed-in arrays.
  5. 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. }
  19. module.exports = intersection;