diff --git a/lodash.js b/lodash.js index 97756a557..2f99ce63c 100644 --- a/lodash.js +++ b/lodash.js @@ -7247,13 +7247,14 @@ * Resolves the value of property `key` on `object`. If `key` is a function * it will be invoked with the `this` binding of `object` and its result returned, * else the property value is returned. If `object` is falsey then `undefined` - * is returned. + * is returned. If property `key` is not set, return defaultValue if defined; * * @static * @memberOf _ * @category Utilities * @param {Object} object The object to inspect. * @param {string} key The name of the property to resolve. + * @param {*} [defaultValue] The value to return if object doesn't have key. * @returns {*} Returns the resolved value. * @example * @@ -7269,10 +7270,13 @@ * * _.result(object, 'stuff'); * // => 'nonsense' + + * _.result(object, 'pizza', 'spaghetti'); + * // => 'spaghetti' */ - function result(object, key) { + function result(object, key, defaultValue) { if (object) { - var value = object[key]; + var value = typeof object[key] !== 'undefined' ? object[key] : defaultValue; return isFunction(value) ? object[key]() : value; } } diff --git a/test/test.js b/test/test.js index bee68d775..e7f675733 100644 --- a/test/test.js +++ b/test/test.js @@ -6600,6 +6600,17 @@ deepEqual(actual, expected); }); + + test('should return the default value if key is not found', 2, function() { + var object = { + 'a': 1, + 'b': 2 + } + + strictEqual(_.result(object, 'c', 3), 3); + strictEqual(_.result(object, 'c'), undefined); + }); + }()); /*--------------------------------------------------------------------------*/