Lodash - update 方法



語法

_.update(object, path, updater)

該方法就像 _.set,不同之處在於它接受更新程式來生成要設定的值。使用 _.updateWith 來自定義路徑建立。更新程式使用一個引數呼叫:(value)。

引數

  • object (物件) − 要修改的物件。

  • path (陣列或字串) − 要設定的屬性的路徑。

  • updater (函式) − 加法中的第二個數字。

輸出

  • (物件) − 返回物件。

示例

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
lodash_object.htm
廣告
© . All rights reserved.