update.js (1076B)
1 var baseUpdate = require('./_baseUpdate'), 2 castFunction = require('./_castFunction'); 3 4 /** 5 * This method is like `_.set` except that accepts `updater` to produce the 6 * value to set. Use `_.updateWith` to customize `path` creation. The `updater` 7 * is invoked with one argument: (value). 8 * 9 * **Note:** This method mutates `object`. 10 * 11 * @static 12 * @memberOf _ 13 * @since 4.6.0 14 * @category Object 15 * @param {Object} object The object to modify. 16 * @param {Array|string} path The path of the property to set. 17 * @param {Function} updater The function to produce the updated value. 18 * @returns {Object} Returns `object`. 19 * @example 20 * 21 * var object = { 'a': [{ 'b': { 'c': 3 } }] }; 22 * 23 * _.update(object, 'a[0].b.c', function(n) { return n * n; }); 24 * console.log(object.a[0].b.c); 25 * // => 9 26 * 27 * _.update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; }); 28 * console.log(object.x[0].y.z); 29 * // => 0 30 */ 31 function update(object, path, updater) { 32 return object == null ? object : baseUpdate(object, path, castFunction(updater)); 33 } 34 35 module.exports = update;