result source npm

_.result(object, path, [defaultValue])

This method is like _.get except that if the resolved value is a function it's invoked with the this binding of its parent object and its result is returned.

Arguments

  1. object (Object)

    The object to query.

  2. path (Array|string)

    The path of the property to resolve.

  3. [defaultValue] (*)

    The value returned if the resolved value is undefined.

Returns (*)

Returns the resolved value.

Example

var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };

_.result(object, 'a[0].b.c1');
// => 3

_.result(object, 'a[0].b.c2');
// => 4

_.result(object, 'a.b.c', 'default');
// => 'default'

_.result(object, 'a.b.c', _.constant('default'));
// => 'default'