1 var castPath = require('./_castPath'),
2 isFunction = require('./isFunction'),
3 toKey = require('./_toKey');
6 * This method is like `_.get` except that if the resolved value is a
7 * function it's invoked with the `this` binding of its parent object and
8 * its result is returned.
14 * @param {Object} object The object to query.
15 * @param {Array|string} path The path of the property to resolve.
16 * @param {*} [defaultValue] The value returned for `undefined` resolved values.
17 * @returns {*} Returns the resolved value.
20 * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
22 * _.result(object, 'a[0].b.c1');
25 * _.result(object, 'a[0].b.c2');
28 * _.result(object, 'a[0].b.c3', 'default');
31 * _.result(object, 'a[0].b.c3', _.constant('default'));
34 function result(object, path, defaultValue) {
35 path = castPath(path, object);
40 // Ensure the loop is entered when path is empty.
45 while (++index < length) {
46 var value = object == null ? undefined : object[toKey(path[index])];
47 if (value === undefined) {
51 object = isFunction(value) ? value.call(object) : value;
56 module.exports = result;