_.set(object, path, value)
Sets the value at path
of object
. If a portion of path
doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties. Use _.setWith
to customize path
creation.Note: This method mutates object
.
Since
3.7.0
Arguments
object
(Object): The object to modify.path
(Array|string): The path of the property to set.value
(*): The value to set.
Returns
(Object): Returns object
.
Example
var object = { 'a': [{ 'b': { 'c': 3 } }] };_.set(object, 'a[0].b.c', 4);console.log(object.a[0].b.c);// => 4_.set(object, ['x', '0', 'y', 'z'], 5);console.log(object.x[0].y.z);// => 5
当前内容版权归 lodash.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 lodash.com .