主题
set
js
_.set(object, path, value)
设置 object
的 path
处的值。如果 path
的一部分不存在,则创建它。为缺失的索引属性创建数组,而为所有其他缺失的属性创建对象。使用 _.setWith
自定义 path
创建。
¥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.
注意:此方法改变 object
。
¥Note: This method mutates object
.
新增于
¥Since
3.7.0
参数
¥Arguments
object
(对象):要查询的对象。¥
object
(Object): The object to modify.path
(数组|字符串):要设置的属性的路径。¥
path
(Array|string): The path of the property to set.value
(*):要设置的值。¥
value
()*: The value to set.
返回
¥Returns
(对象):返回 object
。
¥(Object): Returns object
.
示例
¥Example
js
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