set.js (960B)
1 var baseSet = require('./_baseSet'); 2 3 /** 4 * Sets the value at `path` of `object`. If a portion of `path` doesn't exist, 5 * it's created. Arrays are created for missing index properties while objects 6 * are created for all other missing properties. Use `_.setWith` to customize 7 * `path` creation. 8 * 9 * **Note:** This method mutates `object`. 10 * 11 * @static 12 * @memberOf _ 13 * @since 3.7.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 {*} value The value to set. 18 * @returns {Object} Returns `object`. 19 * @example 20 * 21 * var object = { 'a': [{ 'b': { 'c': 3 } }] }; 22 * 23 * _.set(object, 'a[0].b.c', 4); 24 * console.log(object.a[0].b.c); 25 * // => 4 26 * 27 * _.set(object, ['x', '0', 'y', 'z'], 5); 28 * console.log(object.x[0].y.z); 29 * // => 5 30 */ 31 function set(object, path, value) { 32 return object == null ? object : baseSet(object, path, value); 33 } 34 35 module.exports = set;