property.js 799 B

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