How to Merge Array of Objects by Property using Lodash?
Last Updated :
22 Aug, 2024
Merging an array of objects by property using Lodash is a common operation when you need to consolidate data that share a common identifier. For example, if you have a list of user objects and you want to combine them into a single object based on a unique property, Lodash provides useful functions to simplify this process.
Below are different approaches to merging objects in an array by a specific property using Lodash:
Using _.merge( ) and _.groupBy ( )
In this approach, we first group the objects in the array based on a common property using _.groupBy ( ). Then, we merge the objects within each group using _.merge ( ). This method is useful when we have multiple objects that should be combined into one based on a shared key.
Syntax:
const groupedObjects = _.groupBy(array, 'property');
const mergedObjects = _.mapValues(groupedObjects, group => _.merge({}, ...group));
Example: This code groups an array of student objects by their id
and merges the objects within each group into a single object, consolidating information for each student.
JavaScript
// Import lodash using CommonJS require
const _ = require('lodash');
const students = [
{ id: 1, name: 'Geek', grade: 'A' },
{ id: 2, name: 'Geekina', grade: 'B' },
{ id: 1, sports: 'Soccer' },
{ id: 2, sports: 'Basketball' }
];
// Group students by 'id'
const groupedStudents = _.groupBy(students, 'id');
// Merge objects within each group
const mergedStudents =
_.mapValues(groupedStudents, group =>
_.merge({}, ...group));
console.log(mergedStudents);
Output:
Using _.keyBy ( ) and _.assign ( )
In this approach, we first use _.keyBy ( ) to turn the array of objects into an object, where each key is the value of the specified property. Then, we use _.assign ( ) or _.merge ( ) to combine these objects. This method is efficient and works well when we want to quickly merge objects by a property.
Syntax:
const keyedObjects = _.keyBy(array, 'property');
const mergedObject = _.assign({}, keyedObjects);
Example: This code keys an array of student objects by their id
and merges the keyed objects into a single object, consolidating the information for each student.
JavaScript
// Import lodash using CommonJS require
const _ = require('lodash');
const students = [
{ id: 1, name: 'Geek', grade: 'A' },
{ id: 2, name: 'Geekina', grade: 'B' },
{ id: 1, sports: 'Soccer' },
{ id: 2, sports: 'Basketball' }
];
// Key the students by 'id'
const keyedStudents = _.keyBy(students, 'id');
// Merge the keyed student objects
const mergedStudent = _.assign({}, keyedStudents);
console.log(mergedStudent);
Output:
How to Merge Array of Objects by Property using Lodash?
Similar Reads
How to Find Property by Name in a Deep Object Using Lodash? When working with deeply nested objects in JavaScript, finding a specific property can be challenging. Using Lodash, a powerful utility library, simplifies this task with its robust set of functions. This guide explores how to effectively search for a property by name within a deeply nested object u
2 min read
How to Replace Objects in Array using Lodash? Replacing objects in an array using Lodash typically involves identifying the specific object(s) you want to replace and then performing the replacement operation. Lodash provides a range of methods to facilitate such operations, although the actual replacement might involve combining Lodash functio
3 min read
How to Filter Array of Objects with Lodash Based on Property Value? Sometimes, We need to filter an array of objects based on specific property values. Lodash, a powerful utility library, provides efficient methods for such operations. we will explore how to use Lodash to filter an array of objects by property value, ensuring that you can easily extract and work wit
2 min read
How to Convert Object Array to Hash Map using Lodash ? Converting an Object Array to a Hash Map consists of manipulating the array elements to create a key-value mapping. Below are the different approaches to Converting an object array to a hash map using Lodash:Table of Content Using keyBy() functionUsing reduce functionRun the below command before run
2 min read
How to Compare Two Objects using Lodash? To compare two objects using lodash, we employ Lodash functions such as _.isEqual(), _.isMatch(), and _.isEqualWith(). These methods enable us to do a comparison between two objects and determine if they are equivalent or not.Below are the approaches to do a comparison between two objects using Loda
4 min read