How to Merge two Different Arrays of Objects with Unique Values in JavaScript?
Last Updated :
05 Jul, 2024
Merging two arrays of objects with unique values is a common task in JavaScript. This operation involves combining the elements of two arrays while ensuring that each resulting object has a unique identifier. We will explore various approaches to merging two arrays of objects with unique values.
These are the following approaches:
Approach 1: Using concat() and filter() Methods
- The concat() method is used to merge array1 and the filtered values from array2 to create a new array.
- The filter() method is applied to array2, ensuring that only objects not present in array1 (based on the id property) are included.
- The resulting array, mergedArray, contains unique objects from both arrays.
Example: This example shows the implementation of the above-explained approach.
JavaScript
const array1 = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
];
const array2 = [
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
];
const mergedArray = array1.concat(array2.filter(
item2 => !array1.some(item1 => item1.id === item2.id)));
console.log(mergedArray);
Output[
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
]
Approach 2: Using reduce() Method
- The reduce() method is used to iteratively merge array2 into array1.
- The some() method is employed to check if an object with the same id already exists in the accumulated array.
- If the id is not found, the object is pushed to the accumulator, resulting in a merged array with unique objects.
Example: This example shows the implementation of the above-explained approach.
JavaScript
const array1 = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
];
const array2 = [
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
];
// Merging arrays with unique values
// using reduce() method
const mergedArray = array2
.reduce((accumulator, item2) => {
if (!accumulator.some(item1 =>
item1.id === item2.id)) {
accumulator.push(item2);
}
return accumulator;
}, array1);
console.log(mergedArray);
Output[
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
]
Approach 3: Using Map
- A Map is used to store the unique objects based on their id properties.
- The Set is employed to ensure uniqueness, preventing duplicate objects with the same id.
- Finally, Array.from() is used to convert the values of the Map back into an array.
Example: This example shows the implementation of the above-explained approach.
JavaScript
const array1 = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
];
const array2 = [
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
];
// Merging arrays with unique values
// using Map and Set
const map = new Map([...array1, ...array2]
.map(obj => [obj.id, obj]));
const mergedArray = Array.from(map.values());
console.log(mergedArray);
Output[
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
]
Approach 4: Using filter() and some() Methods
- The some() method is used to check if there is any object in array1 with the same id as the current object in array2.
- The filter() method is applied to array2 to only include objects that are not present in array1 (based on the id property).
- The final array, resultArray, contains unique objects from both arrays.
Example: This example shows the implementation of the above-explained approach.
JavaScript
const array1 = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
];
const array2 = [
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
];
const mergedArray = array2.filter(
item2 => !array1.some(item1 => item1.id === item2.id)
);
const resultArray = [...array1, ...mergedArray];
console.log(resultArray);
Output[
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
]
Approach 5: Using filter(), Map and has() Methods
- A Map is created from array1, with the id as the key and the object as the value.
- The has() method is used to check if there is any object in the Map with the same id as the current object in array2.
- The filter() method is applied to array2 to only include objects that are not present in the Map (based on the id property).
- The final array, resultArray, contains unique objects from both arrays.
Example: This example shows the implementation of the above-explained approach.
JavaScript
const array1 = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
];
const array2 = [
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
];
const map = new Map(array1.map(obj => [obj.id, obj]));
const mergedArray = array2.filter(item2 => !map.has(item2.id));
const resultArray = [...map.values(), ...mergedArray];
console.log(resultArray);
Output[
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
]
Similar Reads
How to Swap Two Array of Objects Values using JavaScript ? Swapping values between two arrays of objects is a common operation in JavaScript, there are multiple methods to swap values between arrays of objects efficiently. Swapping values between arrays can be useful in scenarios like reordering data, shuffling items, or performing certain algorithms. These
6 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ? Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsi
4 min read
How to Deep Merge Two Objects in JavaScript ? Typically, in JavaScript, combining two objects is a commonplace thing to do, but when it involves intricate nesting, deep merging turns out to be necessary, deep merge is the activity of taking the attributes or features from multiple objects (which have nested objects) and creating a new object wi
3 min read
How to Return an Array of Unique Objects in JavaScript ? Returning an array of unique objects consists of creating a new array that contains unique elements from an original array. We have been given an array of objects, our task is to extract or return an array that consists of unique objects using JavaScript approaches. There are various approaches thro
5 min read
How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
How to Convert Array of Objects into Unique Array of Objects in JavaScript ? Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has v
8 min read
JavaScript - How To Get Distinct Values From an Array of Objects? Here are the different ways to get distinct values from an array of objects in JavaScript1. Using map() and filter() MethodsThis approach is simple and effective for filtering distinct values from an array of objects. You can use map() to extract the property you want to check for uniqueness, and th
4 min read
How to filter out the non-unique values in an array using JavaScript ? In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements. These are the following ways by which we can filter out the non-unique values in an arra
4 min read
How to Create Array of Objects From Keys & Values of Another Object in JavaScript ? Creating an array of objects from the keys and values of another object involves transforming the key-value pairs of the original object into individual objects within an array. Each object in the array represents a key-value pair from the source object. Below approaches can be used to accomplish th
3 min read
How to compare Arrays of Objects in JavaScript? In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations.Syntax: Before going to detail the comparison techniques, let's first understand h
5 min read