pickBy.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var arrayMap = require('./_arrayMap'),
  2. baseIteratee = require('./_baseIteratee'),
  3. basePickBy = require('./_basePickBy'),
  4. getAllKeysIn = require('./_getAllKeysIn');
  5. /**
  6. * Creates an object composed of the `object` properties `predicate` returns
  7. * truthy for. The predicate is invoked with two arguments: (value, key).
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 4.0.0
  12. * @category Object
  13. * @param {Object} object The source object.
  14. * @param {Function} [predicate=_.identity] The function invoked per property.
  15. * @returns {Object} Returns the new object.
  16. * @example
  17. *
  18. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  19. *
  20. * _.pickBy(object, _.isNumber);
  21. * // => { 'a': 1, 'c': 3 }
  22. */
  23. function pickBy(object, predicate) {
  24. if (object == null) {
  25. return {};
  26. }
  27. var props = arrayMap(getAllKeysIn(object), function(prop) {
  28. return [prop];
  29. });
  30. predicate = baseIteratee(predicate);
  31. return basePickBy(object, props, function(value, path) {
  32. return predicate(value, path[0]);
  33. });
  34. }
  35. module.exports = pickBy;