property.js 793 B

123456789101112131415161718192021222324252627282930313233
  1. var baseProperty = require('./_baseProperty'),
  2. basePropertyDeep = require('./_basePropertyDeep'),
  3. isKey = require('./_isKey'),
  4. toKey = require('./_toKey');
  5. /**
  6. * Creates a function that returns the value at `path` of a given object.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 2.4.0
  11. * @category Util
  12. * @param {Array|string} path The path of the property to get.
  13. * @returns {Function} Returns the new accessor function.
  14. * @example
  15. *
  16. * var objects = [
  17. * { 'a': { 'b': 2 } },
  18. * { 'a': { 'b': 1 } }
  19. * ];
  20. *
  21. * _.map(objects, _.property('a.b'));
  22. * // => [2, 1]
  23. *
  24. * _.map(_.sortBy(objects, _.property(['a', 'b'])), 'a.b');
  25. * // => [1, 2]
  26. */
  27. function property(path) {
  28. return isKey(path) ? baseProperty(toKey(path)) : basePropertyDeep(path);
  29. }
  30. module.exports = property;