Open In App

Lodash _.assignInWith() Method

Last Updated : 03 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.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: 

  • 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:

  • 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.

Example 1: In this example, we are changing the value to the object by passing the source, destination object, and the modified value in the lodash _.assignInWith() method.

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: In this example, we are changing the value to the function’s properties by passing the source, destination object, and the modified value in the lodash _.assignInWith() method.

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



Previous Article
Next Article

Similar Reads

Lodash _.method() Method
Lodash _.method() method creates a function that invokes the method at the path of a given object. Any additional arguments are provided to the invoked method. Syntax: _.method(path, args);Parameters: path: This is the path to invoke.args: These are the arguments to invoke the method with.Return Value: This method returns the new invoker function.
1 min read
Lodash _.flattenDeep() and _.flattenDepth() Method
Lodash _.flattenDeep() Method The _.flattenDeep() method is used to completely flatten nested arrays. It does this recursively. Syntax: _.flattenDeep( array ) Parameters: This method accepts single parameter as mentioned above and described below: array: This parameter holds the array that to be flatten. Return Value: This method returns the new fl
2 min read
Lodash _.dropWhile() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers etc. The Loadsh.dropWhile() method is used to return the slice of the given array. This takes a predicate function that iterate through each element of the array and if the function returns false, it returned the sli
2 min read
Lodash _.stubFalse() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.stubFalse() method is used to always return a false value. The basic difference between using a normal false value and using the _.stubFalse() method is that lambda functions create a new different functi
1 min read
Lodash _.pullAllBy() Method
The _.pullAllBy() method is used to remove the values from the original array by iterating over each element in the array by using the Iteratee function. It is almost the same as _.pullAll() function. Syntax: _.pullAllBy(array, values, [iteratee=_.identity]) Parameters: This method accepts two parameters as mentioned above and described below: arra
2 min read
Lodash _.sortedLastIndexOf() Method
The _.sortedLastIndexOf method is used to return the highest index of the array where an element can be inserted and maintain its sorted order. Also this method is like _.lastIndexOf except that it performs a binary search on a sorted array.Syntax: _.sortedLastIndexOf(array, value) Parameters: This method accepts two parameters as mentioned above a
1 min read
Lodash _.isSymbol() Method
The _.isSymbol() method is used to find whether the given value is a symbol object or not. It returns True if the given value is a symbol object. Otherwise, it returns false. Syntax: _.isSymbol(value) Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter holds the value to check. Return Val
1 min read
Lodash _.toArray() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.toArray() method is used to convert the given value to an array. Syntax: _.toArray( value ) Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter
1 min read
Lodash _.reduce() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc. The _.reduce() method reduces collection to value which is accumulated result of running each element in the collection through iteratee, where each successive invocation is supplied return value of
2 min read
Lodash _.splitAt() Method
The Lodash _.splitAt() method takes an array and a numeric index and returns a new array containing two embedded arrays made by splitting the original array at the provided numeric index. Syntax: _.splitAt(array, numeric_index) Parameters: This method takes two parameters as mentioned above and described below: array: The array to be split.numeric_
3 min read