reject.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var arrayFilter = require('./_arrayFilter'),
  2. baseFilter = require('./_baseFilter'),
  3. baseIteratee = require('./_baseIteratee'),
  4. isArray = require('./isArray'),
  5. negate = require('./negate');
  6. /**
  7. * The opposite of `_.filter`; this method returns the elements of `collection`
  8. * that `predicate` does **not** return truthy for.
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 0.1.0
  13. * @category Collection
  14. * @param {Array|Object} collection The collection to iterate over.
  15. * @param {Function} [predicate=_.identity] The function invoked per iteration.
  16. * @returns {Array} Returns the new filtered array.
  17. * @see _.filter
  18. * @example
  19. *
  20. * var users = [
  21. * { 'user': 'barney', 'age': 36, 'active': false },
  22. * { 'user': 'fred', 'age': 40, 'active': true }
  23. * ];
  24. *
  25. * _.reject(users, function(o) { return !o.active; });
  26. * // => objects for ['fred']
  27. *
  28. * // The `_.matches` iteratee shorthand.
  29. * _.reject(users, { 'age': 40, 'active': true });
  30. * // => objects for ['barney']
  31. *
  32. * // The `_.matchesProperty` iteratee shorthand.
  33. * _.reject(users, ['active', false]);
  34. * // => objects for ['fred']
  35. *
  36. * // The `_.property` iteratee shorthand.
  37. * _.reject(users, 'active');
  38. * // => objects for ['barney']
  39. */
  40. function reject(collection, predicate) {
  41. var func = isArray(collection) ? arrayFilter : baseFilter;
  42. return func(collection, negate(baseIteratee(predicate, 3)));
  43. }
  44. module.exports = reject;