The default value is Lodash _.result ()

Why does the lodash result method return the default value in this case?

Arguments object (Object): the object to query.

key (string): property key to resolve.

[defaultValue] (*): Return value if property value is resolved to undefined.

var result = _.result({ foo: 1 }, 'bar', 'default');

console.log(typeof _.result({ foo: 1 }, 'bar') === 'undefined') // true

console.log(result); // expected: 'default'

http://jsfiddle.net/dbvs5ney/

+4
source share
2 answers

It seems that the parameter defaultwas added only in version 3.0.0
Compare implementation _.result:
3.0.0 lodash.js

function result(object, key, defaultValue) {
  var value = object == null ? undefined : object[key];
  if (typeof value == 'undefined') {
    value = defaultValue;
  }
  return isFunction(value) ? value.call(object) : value;
}

And 2.2.1 lodash.js :

function result(object, property) {
  if (object) {
    var value = object[property];
    return isFunction(value) ? object[property]() : value;
  }
}
+5
source

, . lodash. 2.2.1?

+2

All Articles