reject.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var arrayFilter = require('./_arrayFilter'),
  2. baseFilter = require('./_baseFilter'),
  3. baseIteratee = require('./_baseIteratee'),
  4. isArray = require('./isArray');
  5. /**
  6. * The opposite of `_.filter`; this method returns the elements of `collection`
  7. * that `predicate` does **not** return truthy for.
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 0.1.0
  12. * @category Collection
  13. * @param {Array|Object} collection The collection to iterate over.
  14. * @param {Array|Function|Object|string} [predicate=_.identity]
  15. * 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. predicate = baseIteratee(predicate, 3);
  43. return func(collection, function(value, index, collection) {
  44. return !predicate(value, index, collection);
  45. });
  46. }
  47. module.exports = reject;