* Sets the property value of `path` on `object`. If a portion of `path` * does not exist it is created. * * @static * @memberOf _ * @category Object * @param {Object} object The object to augment. * @param {Array|string} path The path of the property to set. *
(object, path, value)
| 11320 | * // => 5 |
| 11321 | */ |
| 11322 | function set(object, path, value) { |
| 11323 | if (object == null) { |
| 11324 | return object; |
| 11325 | } |
| 11326 | var pathKey = (path + ''); |
| 11327 | path = (object[pathKey] != null || isKey(path, object)) ? [pathKey] : toPath(path); |
| 11328 | |
| 11329 | var index = -1, |
| 11330 | length = path.length, |
| 11331 | endIndex = length - 1, |
| 11332 | nested = object; |
| 11333 | |
| 11334 | while (nested != null && ++index < length) { |
| 11335 | var key = path[index]; |
| 11336 | if (isObject(nested)) { |
| 11337 | if (index == endIndex) { |
| 11338 | nested[key] = value; |
| 11339 | } else if (nested[key] == null) { |
| 11340 | nested[key] = isIndex(path[index + 1]) ? [] : {}; |
| 11341 | } |
| 11342 | } |
| 11343 | nested = nested[key]; |
| 11344 | } |
| 11345 | return object; |
| 11346 | } |
| 11347 | |
| 11348 | /** |
| 11349 | * An alternative to `_.reduce`; this method transforms `object` to a new |