Lodash - update 方法
语法
_.update(object, path, updater)
此方法与 _.set 类似,不同之处在于它接受 updater 来生成要设置的值。使用 _.updateWith 自定义路径创建。使用一个参数调用更新程序:(value)。
参数
object (Object) − 要修改的对象。
path (Array|string) − 要设置的属性的路径。
updater (Function) −加法中的第二个数字。
输出
(Object) − 返回对象。
示例
var _ = require('lodash'); var object = { 'a': [{ 'b': { 'c': 3 } }] }; _.update(object, 'a[0].b.c', function(n) { return n * n; }); console.log(object.a[0].b.c);
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
9