1 var isIndex = require('../internal/isIndex'),
2 isKey = require('../internal/isKey'),
3 isObject = require('../lang/isObject'),
4 toPath = require('../internal/toPath');
7 * Sets the property value of `path` on `object`. If a portion of `path`
8 * does not exist it's created.
13 * @param {Object} object The object to augment.
14 * @param {Array|string} path The path of the property to set.
15 * @param {*} value The value to set.
16 * @returns {Object} Returns `object`.
19 * var object = { 'a': [{ 'b': { 'c': 3 } }] };
21 * _.set(object, 'a[0].b.c', 4);
22 * console.log(object.a[0].b.c);
25 * _.set(object, 'x[0].y.z', 5);
26 * console.log(object.x[0].y.z);
29 function set(object, path, value) {
33 var pathKey = (path + '');
34 path = (object[pathKey] != null || isKey(path, object)) ? [pathKey] : toPath(path);
38 lastIndex = length - 1,
41 while (nested != null && ++index < length) {
42 var key = path[index];
43 if (isObject(nested)) {
44 if (index == lastIndex) {
46 } else if (nested[key] == null) {
47 nested[key] = isIndex(path[index + 1]) ? [] : {};