![]() |
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 through which it can be performed which are as follows: Table of Content Using SetThis method uses Set to extract unique objects by first converting each of the objects into the JSON string, then mapping it back to the objects. The output array contains the unique objects. Syntax:let mySet = new Set([iterable]); Example: The below code uses a set to return an array of unique objects.
Output [ { articleId: 101, title: 'Introduction to JavaScript' }, { articleId: 102, title: 'Arrays in JavaScript' }, { articleId: 103, title: 'Functions in JavaScript' } ] Using filter MethodThis method uses the filter method in JavaScript to return only the first occurrence of each unique object in the input array, using JSON.stringify for the comparison. The output array has the unique objects stored. Syntax:let newArray = array.filter(callback(element, index, array)); Example: The below code uses filter and indexOf methods to return an array of unique objects.
Output [ { articleId: 101, title: 'Introduction to JavaScript' }, { articleId: 102, title: 'Arrays in JavaScript' }, { articleId: 103, title: 'Functions in JavaScript' } ] Using reduce MethodThe method uses the reduce method from JavaScript to accumulate unique objects from the input array by checking for the existing objects using JSON.stringify. The output array stores the unique objects and is printed using the log() function. Syntax:let result = array.reduce(callback(accumulator, currentValue, index, array), initialValue); Example: The below code uses the reduce method to return an array of unique objects.
Output [ { articleId: 101, title: 'Introduction to JavaScript' }, { articleId: 102, title: 'Arrays in JavaScript' }, { articleId: 103, title: 'Functions in JavaScript' } ] We use the some method to check if the current object’s articleId already exists in the accumulator. And If the articleId is not found in the accumulator, we push the current object obj into the accumulator. Thus accumulator acc contains only unique objects based on the articleId Using map MethodThis method uses the map method along with callback to get unique objects by associating each object with the JSON string. The output array consists of unique objects and is printed using the log() function. Syntax:let newArray = array.map(callback(currentValue, index, array)); Example: The below code uses the map method to return an array of unique objects.
Output [ { articleId: 101, title: 'Introduction to JavaScript' }, { articleId: 102, title: 'Arrays in JavaScript' }, { articleId: 103, title: 'Functions in JavaScript' } ] Using Object Key for Tracking Unique ObjectsThis method uses an object to track and store unique objects based on a specified attribute. The keys of the object are the values of the specified attribute, ensuring uniqueness. Syntax:let newArray = array.reduce((acc, current) => { Example:
Output [ { articleId: 101, title: 'Introduction to JavaScript' }, { articleId: 102, title: 'Arrays in JavaScript' }, { articleId: 103, title: 'Functions in JavaScript' } ] |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 10 |