get.js 884 B

123456789101112131415161718192021222324252627282930313233
  1. var baseGet = require('./_baseGet');
  2. /**
  3. * Gets the value at `path` of `object`. If the resolved value is
  4. * `undefined`, the `defaultValue` is returned in its place.
  5. *
  6. * @static
  7. * @memberOf _
  8. * @since 3.7.0
  9. * @category Object
  10. * @param {Object} object The object to query.
  11. * @param {Array|string} path The path of the property to get.
  12. * @param {*} [defaultValue] The value returned for `undefined` resolved values.
  13. * @returns {*} Returns the resolved value.
  14. * @example
  15. *
  16. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  17. *
  18. * _.get(object, 'a[0].b.c');
  19. * // => 3
  20. *
  21. * _.get(object, ['a', '0', 'b', 'c']);
  22. * // => 3
  23. *
  24. * _.get(object, 'a.b.c', 'default');
  25. * // => 'default'
  26. */
  27. function get(object, path, defaultValue) {
  28. var result = object == null ? undefined : baseGet(object, path);
  29. return result === undefined ? defaultValue : result;
  30. }
  31. module.exports = get;