Lodash’s `merge()` Function
In this post, we will give you information about Lodash’s `merge()` Function. Here we will give you detail about Lodash’s `merge()` Function And how to use it also give you a demo for it if it is necessary.
Given two objects destination
and source
, Lodash’s merge()
function copies the 2nd object’s own properties and inherited properties into the first object.
const destination = { name: 'Will Riker', rank: 'Commander' };
const source = { ship: 'USS Enterprise' };
_.merge(destination, source);
destination.name; // 'Will Riker'
destination.rank; // 'Commander'
destination.ship; // 'USS Enterprise'
Sounds a lot like Object.assign()
, right? While merge()
is very
similar to Object.assign()
and _.assign()
, there are a couple
minor differences.
Differences Between merge()
and assign()
The first detail is that merge()
copies objects recursively,
so _.merge()
is a deep copy
whereas _.assign()
is a shallow copy.
const obj = {
name: {
first: 'Will',
last: 'Riker'
}
};
const deepClone = _.merge({}, obj);
deepClone.name === obj.name; // false
deepClone.name.first = 'Thomas';
obj.name.first; // 'Will'
const shallowClone = _.assign({}, obj);
shallowClone.name === obj.name; // true
shallowClone.name.first = 'Thomas';
obj.name.first; // 'Thomas'
The second detail is how merge()
handles undefined
. If the source
has a key whose value is strictly equal to undefined
, merge()
will
not overwrite that key in the destination
.
let destination = {
firstName: 'Will',
lastName: 'Riker',
rank: 'Commander'
};
// Since 'source.rank' is undefined, 'merge()' won't overwrite
// 'destination.rank'.
_.merge(destination, { firstName: 'Thomas', rank: undefined });
destination.firstName; // 'Thomas'
destination.rank; // 'Commander'
destination = {
firstName: 'Will',
lastName: 'Riker',
rank: 'Commander'
};
// But '_.assign()' and 'Object.assign()' overwrite 'destination.rank'.
_.assign(destination, { firstName: 'Thomas', rank: undefined });
destination.firstName; // 'Thomas'
destination.rank; // undefined
Another difference comes in when you consider how merge()
handles classes.
class Ship {};
Ship.prototype.shipName = 'USS Enterprise';
const ship = new Ship();
// 'merge()' copies inherited properties, so it will copy
// 'shipName'
const merged = _.merge({ name: 'Will Riker', rank: 'Commander' }, ship);
merged.shipName; // 'USS Enterprise'
// 'assign()' does **not** copy inherited properties.
const assigned = Object.assign({ name: 'Will Riker', rank: 'Commander' }, ship);
assigned.shipName; // undefined
Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm. Lodash is a JavaScript library that helps programmers write more concise and maintainable JavaScript. It can be broken down into several main areas: Utilities: for simplifying common programming tasks such as determining type as well as simplifying math operations.
Why Lodash?
Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.Lodash’s modular methods are great for:
- Iterating arrays, objects, & strings
- Manipulating & testing values
- Creating composite functions
Hope this code and post will helped you for implement Lodash’s `merge()` Function. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs