Lodash教程

Lodash setWith method

语法

_.setWith(object, path, value, [customizer])
This method is like _.set except that it accepts customizer which is invoked to produce the objects of path. If customizer returns undefined path creation is handled by the method instead. The customizer is invoked with three arguments: (nsValue, key, nsObject).

参数

object (Object) − The object to modify. path (Array|string) − The path of the property to set. value (*) − The value to set [customizer] (Function) − The function to customize assigned values.

输出

(Object) − Returns object.

实例

var _ = require('lodash');
var object = { 'a': [{ 'b': { 'c': 3 } }] };
 
_.setWith(object, '[0][1]', 'a', Object)
console.log(object);
Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

输出

{ '0': { '1': 'a' }, a: [ { b: [Object] } ] }
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4