One way to achieve this is by using _.assignWith
or _.assignInWith
. The notable distinction lies in the fact that while assignIn
traverses the prototype chain, with plain objects, both functions yield the same result.
In either scenario, a customizer function can be supplied to determine how object values are merged. For example:
const combineValues = (srcValue, objValue) =>
srcValue + objValue;
This simple implementation sums up values for corresponding keys:
const fruits = {
apple: 2,
orange: 3,
grape: 4,
banana: 5
}
const combineValues = (srcValue, objValue) =>
srcValue + objValue;
const modification = {apple: 2, banana: 3};
const premiumFrutis = _.assignWith(fruits, modification, combineValues);
console.log(premiumFrutis);
// {
// apple: 4,
// orange: 3,
// grape: 4,
// banana: 8
// }
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84e8ebe0e5f7ecc4b0aab5b3aab6b5">[email protected]</a>/lodash.min.js"></script>
Nevertheless, implementing such a custom function may not even be necessary since Lodash already offers _.add
, ready to be employed directly:
const fruits = {
apple: 2,
orange: 3,
grape: 4,
banana: 5
}
const modification = {apple: 2, banana: 3};
const premiumFrutis = _.assignWith(fruits, modification, _.add);
console.log(premiumFrutis);
// {
// apple: 4,
// orange: 3,
// grape: 4,
// banana: 8
// }
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff93909b9e8c97bfcbd1cec8d1cdce">[email protected]</a>/lodash.min.js"></script>
The capability extends to subtraction if negative values are provided:
const fruits = {
apple: 2,
orange: 3,
grape: 4,
banana: 5
}
const modification = {apple: 2, banana: -1};
const premiumFrutis = _.assignWith(fruits, modification, _.add);
console.log(premiumFrutis);
// {
// apple: 4,
// orange: 3,
// grape: 4,
// banana: 4
// }
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="600c0f0401130820544e51574e5251">[email protected]</a>/lodash.min.js"></script>
An issue may arise when more modifications are provided than there are keys in the original objects:
const fruits = {
apple: 2,
orange: 3,
grape: 4,
banana: 5
}
const modification = {apple: 2, banana: 3, lemon: 42};
const premiumFrutis = _.assignWith(fruits, modification, _.add);
console.log(premiumFrutis);
// {
// apple: 4,
// orange: 3,
// grape: 4,
// banana: 8,
// lemon: 42
// }
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="55393a3134263d15617b64627b6764">[email protected]</a>/lodash.min.js"></script>
If no new items need to be added, _.pickBy
could be utilized to eliminate any modifications not present in the initial object:
const fruits = {
apple: 2,
orange: 3,
grape: 4,
banana: 5
}
const modification = {apple: 2, banana: 3, lemon: 42};
const onlyExisting = _.pickBy(modification, (value, key) => key in fruits);
const premiumFrutis = _.assignWith(fruits, onlyExisting, _.add);
console.log(premiumFrutis);
// {
// apple: 4,
// orange: 3,
// grape: 4,
// banana: 8
// }
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2cecdc6c3d1cae2968c93958c9093">[email protected]</a>/lodash.min.js"></script>