乐闻世界logo
搜索文章和话题

How to merge two arrays and sum values of duplicate objects using lodash

1个答案

1

When using Lodash to merge two arrays, if the arrays contain objects that may duplicate across different arrays, we can use the _.unionWith function to merge these two arrays by passing a custom comparison function to determine when two objects should be considered identical. Once the objects are identified as identical, we can sum specific properties of these objects.

Here is a specific example:

javascript
var array1 = [ { id: 1, value: 10 }, { id: 2, value: 20 }, { id: 3, value: 30 } ]; var array2 = [ { id: 2, value: 25 }, { id: 3, value: 35 }, { id: 4, value: 45 } ];

In this example, we want to merge array1 and array2, summing the value for objects with the same id.

We can implement this with the following code:

javascript
var _ = require('lodash'); function mergeObjectsWithSum(array1, array2) { return _.unionWith(array1, array2, (a, b) => { if (a.id === b.id) { a.value += b.value; return true; } return false; }); } var mergedArray = mergeObjectsWithSum(array1, array2); console.log(mergedArray);

This function mergeObjectsWithSum uses _.unionWith, which accepts array1 and array2 as inputs and defines a comparison function. This comparison function checks if the id of two objects is the same; if so, it adds their value and returns true to indicate that the two objects should be treated as one, thereby achieving the summation.

The output will be:

javascript
[ { id: 1, value: 10 }, { id: 2, value: 45 }, { id: 3, value: 65 }, { id: 4, value: 45 } ]

In this output, you can see that the value for objects with id 2 and 3 have been successfully summed.

When you need to merge two arrays using Lodash and sum values for duplicate objects, you can follow these steps:

  1. Introduce the Lodash Library: First, ensure that the Lodash library is included in your project, as we will use its functions to handle the arrays.
  2. Concatenate Arrays: Use the _.concat function from Lodash to merge the two arrays.
  3. Merge and Sum Values: Use _.groupBy to group the objects in the concatenated array by a specific key (e.g., ID), then use _.map to iterate over the grouped results and sum the values for each group.

Here is a specific example demonstrating how to implement this:

Example Code

javascript
import _ from 'lodash'; const array1 = [{ id: 1, value: 10 }, { id: 2, value: 20 }]; const array2 = [{ id: 1, value: 15 }, { id: 3, value: 25 }]; // Use Lodash's concat method to merge arrays const combinedArray = _.concat(array1, array2); // Use groupBy to group objects by 'id' const groupedObjects = _.groupBy(combinedArray, 'id'); // Use map to iterate over grouped objects and sum values const sumResults = _.map(groupedObjects, (group) => { return { id: group[0].id, value: _.sumBy(group, 'value') // Sum the 'value' for each group }; }); console.log(sumResults); // Output: [{ id: 1, value: 25 }, { id: 2, value: 20 }, { id: 3, value: 25 }]

Explanation

In this process, we first use _.concat to merge the two arrays. Then, we use _.groupBy to group the objects by the id property, so all objects with the same id are grouped together. Finally, we use _.map and _.sumBy to sum the value for each group.

This method is particularly suitable for handling merge and aggregation operations on large datasets. Lodash's functional programming approach makes such operations more concise and efficient.

2024年6月29日 12:07 回复

你的答案