Skip to content

update

js
_.update(object, path, updater)

此方法类似于 _.set,只是它接受 updater 来生成要设置的值。使用 _.updateWith 自定义 path 创建。updater 使用一个参数调用:(value)。

¥This method is like _.set except that accepts updater to produce the value to set. Use _.updateWith to customize path creation. The updater is invoked with one argument: (value).

注意:此方法改变 object

¥Note: This method mutates object.

新增于

¥Since

4.6.0

参数

¥Arguments

  1. object (对象):要查询的对象。

    ¥object (Object): The object to modify.

  2. path (数组|字符串):要设置的属性的路径。

    ¥path (Array|string): The path of the property to set.

  3. updater (函数):生成更新值的函数。

    ¥updater (Function): The function to produce the updated value.

返回

¥Returns

(对象):返回 object

¥(Object): Returns object.

示例

¥Example

js
var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.update(object, 'a[0].b.c', function(n) { return n * n; });
console.log(object.a[0].b.c);
// => 9

_.update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; });
console.log(object.x[0].y.z);
// => 0

Lodash v4.17 中文网 - 粤ICP备13048890号