Lodash教程

Lodash assignWith method

语法

_.assignWith(object, sources, [customizer])
This method is like _.assign except that it accepts customizer which is invoked to produce the assigned values. If customizer returns undefined, assignment is handled by the method instead. The customizer is invoked with five arguments: (objValue, srcValue, key, object, source).

参数

object (Object) − The destination object. sources (...Object) − The source objects. [customizer] (Function) − The function to customize assigned values.

输出

(Object) − Returns object.

实例

var _ = require('lodash');
function customizer(objValue, srcValue) {
   return _.isUndefined(objValue) ? srcValue : objValue;
}
var defaults = _.partialRight(_.assignWith, customizer);
console.log(defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }));
Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

输出

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