![]() |
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 various methods to convert an array of objects into a unique array of objects which are as follows: Table of Content Using SetThis approach uses the Set data structure in JavaScript, which automatically removes duplicate values. By mapping the array of objects to their JSON representations, the Set eliminates duplicate string representations, and then the unique objects are reconstructed. Syntax:
Example: Using Set to create a unique array of objects by stringifying and parsing their JSON representations, eliminating duplicates based on content.
Output Input Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, { id: 1, name: 'Geek1' }, { id: 3, name: 'Geek3' } ] Unique Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, ... Using filter() and indexOf() methodsThe approach uses filter() and indexOf() to create a unique array by checking the index of each object’s first occurrence based on JSON string representation for precise comparison. This ensures the removal of duplicate objects from the original array. Syntax:
Example: Filtering out duplicate objects from the input array using filter() and indexOf() based on JSON string comparison, resulting in a unique array.
Output Input Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, { id: 1, name: 'Geek1' }, { id: 3, name: 'Geek3' } ] Unique Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, ... Using MapThis approach uses a Map to efficiently remove duplicate objects from an array. It iterates through the input array, converts each object to a string using JSON.stringify as the key, and checks if the Map already has that key. If not, it adds the key to the Map and pushes the corresponding object to the unique array. This method ensures that unique objects are retained in the final array. Syntax:
Example: Removing duplicate objects from the input array using a Map to track unique object keys based on their JSON string representation, resulting in a unique array.
Output Input Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, { id: 1, name: 'Geek1' }, { id: 3, name: 'Geek3' } ] Unique Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, ... Using reduce()The below approach uses the reduce() method to create a unique array of objects by comparing each object’s string representation. The accumulator (acc) is updated only if the current object is not already present in the accumulator, effectively removing duplicates based on their stringified form. Syntax:let result = array.reduce((accumulator, currentValue, index, array) => { Example: Using the reduce method, this approach iterates through the input array, checking for duplicate objects based on their stringified representations.
Output Input Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, { id: 1, name: 'Geek1' }, { id: 3, name: 'Geek3' } ] Unique Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, ... Using Lodash LibraryUsing the Lodash library’s uniqWith function with a custom comparison function compares objects for uniqueness. It returns an array of unique objects by comparing their properties or values, allowing for flexible and efficient handling of object uniqueness. Example: In this example we use Lodash’s _.uniqWith and _.isEqual to remove duplicate objects from the array.
Output [ Using some() MethodThis approach uses the some() method to create a unique array of objects. The some() method checks if at least one element in the array passes the test implemented by the provided function. If an object with the same key already exists in the unique array, it is not added again. Syntax: array.some(callback(element[, index[, array]])[, thisArg]) Example: The below example uses the some() method to remove duplicate objects from an array based on a specific key in JavaScript.
Output Input Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, { id: 1, name: 'Geek1' }, { id: 3, name: 'Geek3' } ] Unique Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, ... Using ES6 Find MethodThis approach uses the ES6 find method to create a unique array of objects. The find method returns the first element in the array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. Syntax:array.find(callback(element[, index[, array]])[, thisArg]) Example: The following example uses the find method to remove duplicate objects from an array by checking if an object with the same properties already exists in the unique array.
Output Input Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, { id: 1, name: 'Geek1' }, { id: 3, name: 'Geek3' } ] Unique Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, ... Using reduce() with SetThis approach uses the reduce method along with a Set to track seen object IDs. It ensures that only unique objects (based on a specified key) are added to the result array. Example: Using reduce and Set to remove duplicate objects based on a specific key (e.g., id).
Output Input Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, { id: 1, name: 'Geek1' }, { id: 3, name: 'Geek3' } ] Unique Array: [ { id: 1, name: 'Geek1' }, { id: 2, name: 'Geek2' }, ... |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |