2016-01-28 01:16:24 -08:00
|
|
|
define(['./_baseGet'], function(baseGet) {
|
2015-12-16 17:49:35 -08:00
|
|
|
|
|
|
|
|
/** Used as a safe reference for `undefined` in pre-ES5 environments. */
|
|
|
|
|
var undefined;
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-16 17:53:20 -08:00
|
|
|
* Gets the value at `path` of `object`. If the resolved value is
|
2016-07-24 09:52:04 -07:00
|
|
|
* `undefined`, the `defaultValue` is returned in its place.
|
2015-12-16 17:49:35 -08:00
|
|
|
*
|
|
|
|
|
* @static
|
|
|
|
|
* @memberOf _
|
2016-03-26 00:00:01 -07:00
|
|
|
* @since 3.7.0
|
2015-12-16 17:49:35 -08:00
|
|
|
* @category Object
|
|
|
|
|
* @param {Object} object The object to query.
|
|
|
|
|
* @param {Array|string} path The path of the property to get.
|
2016-03-26 00:00:01 -07:00
|
|
|
* @param {*} [defaultValue] The value returned for `undefined` resolved values.
|
2015-12-16 17:49:35 -08:00
|
|
|
* @returns {*} Returns the resolved value.
|
|
|
|
|
* @example
|
|
|
|
|
*
|
|
|
|
|
* var object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
|
|
|
*
|
|
|
|
|
* _.get(object, 'a[0].b.c');
|
|
|
|
|
* // => 3
|
|
|
|
|
*
|
|
|
|
|
* _.get(object, ['a', '0', 'b', 'c']);
|
|
|
|
|
* // => 3
|
|
|
|
|
*
|
|
|
|
|
* _.get(object, 'a.b.c', 'default');
|
|
|
|
|
* // => 'default'
|
|
|
|
|
*/
|
|
|
|
|
function get(object, path, defaultValue) {
|
2015-12-16 17:53:20 -08:00
|
|
|
var result = object == null ? undefined : baseGet(object, path);
|
2015-12-16 17:49:35 -08:00
|
|
|
return result === undefined ? defaultValue : result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return get;
|
|
|
|
|
});
|