Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lodash _.assignInWith() Method

  • Last Updated : 18 Sep, 2020

The _.assignInWith() method of Object in lodash is similar to _.assignIn the method and the only difference is that it accepts customizer which is called in order to generate assigned value. Moreover, if the customizer used here returns undefined then the assignment is dealt with by the method instead.

Note: 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • The customizer used here can be called with five arguments namely objValue, srcValue, key, object, and source.
  • The object used here is altered by this method.

Syntax:

_.assignInWith(object, sources, [customizer])

Parameters: This method accepts three parameters as described below:



  • object: It is the destination object.
  • sources: It is the source of objects.
  • customizer: It is the function that customizes assigned values.

Return Value: This method returns the object.

Below examples illustrate the Lodash _.assignInWith() method in JavaScript:

Example 1:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Defining a function customizer
function customizer(objectVal, sourceVal) {
  return _.isUndefined(objectVal) ? sourceVal : objectVal;
}
  
// Calling assignInWith method with its parameter
let obj = _.assignInWith({ 'gfg': 1 }, { 'geek': 3 }, customizer);
  
// Displays output 
console.log(obj);

Output:

{ gfg: 1, geek: 3 }

Example 2:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Defining a function customizer
function customizer(objectVal, sourceVal) {
  return _.isUndefined(objectVal) ? sourceVal : objectVal;
}
  
// Defining a function GfG
function GfG() {
  this.p = 7;
}
  
// Defining a function Portal
function Portal() {
  this.r = 9;
}
   
// Defining prototype of above functions 
GfG.prototype.q = 8;
Portal.prototype.s = 10;
  
// Calling assignInWith method with its parameter
let obj = _.assignInWith({ 'p': 6 }, 
          new GfG, new Portal,customizer);
  
// Displays output 
console.log(obj);

Output:

{ p: 6, q: 8, r: 9, s: 10 }

Reference: https://lodash.com/docs/4.17.15#assignInWith




My Personal Notes arrow_drop_up