reject.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var arrayFilter = require('../internal/arrayFilter'),
  2. baseCallback = require('../internal/baseCallback'),
  3. baseFilter = require('../internal/baseFilter'),
  4. isArray = require('../lang/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. * @category Collection
  12. * @param {Array|Object|string} collection The collection to iterate over.
  13. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  14. * per iteration.
  15. * @param {*} [thisArg] The `this` binding of `predicate`.
  16. * @returns {Array} Returns the new filtered array.
  17. * @example
  18. *
  19. * _.reject([1, 2, 3, 4], function(n) {
  20. * return n % 2 == 0;
  21. * });
  22. * // => [1, 3]
  23. *
  24. * var users = [
  25. * { 'user': 'barney', 'age': 36, 'active': false },
  26. * { 'user': 'fred', 'age': 40, 'active': true }
  27. * ];
  28. *
  29. * // using the `_.matches` callback shorthand
  30. * _.pluck(_.reject(users, { 'age': 40, 'active': true }), 'user');
  31. * // => ['barney']
  32. *
  33. * // using the `_.matchesProperty` callback shorthand
  34. * _.pluck(_.reject(users, 'active', false), 'user');
  35. * // => ['fred']
  36. *
  37. * // using the `_.property` callback shorthand
  38. * _.pluck(_.reject(users, 'active'), 'user');
  39. * // => ['barney']
  40. */
  41. function reject(collection, predicate, thisArg) {
  42. var func = isArray(collection) ? arrayFilter : baseFilter;
  43. predicate = baseCallback(predicate, thisArg, 3);
  44. return func(collection, function(value, index, collection) {
  45. return !predicate(value, index, collection);
  46. });
  47. }
  48. module.exports = reject;