I am fetching data from an api containing a long list of names. Many of them are duplicates and I want to filter the names so there aren’t any duplicates.
When trying to run the removeDuplicateNames function it just returns an empty array.
const axios = require('axios');
interface Names {
objectId: string;
Name: string;
Gender: string;
createdAt: string;
updatedAt: string;
}
function getNames() {
const options = {
method: 'GET',
url: 'API.COM',
headers: {
'X-Parse-Application-Id': 'xxx',
'X-Parse-REST-API-Key': 'xxx',
},
};
return axios
.request(options)
.then(({ data }: { data: Names[] }) => {
return data; // Return the retrieved data
})
.catch((error: any) => {
console.error(error);
throw error; // Rethrow the error to propagate it to the caller
});
}
function removeDuplicateNames(names) {
return [...new Set(names)];
}
async function main() {
const namesFromApi = await getNames();
const uniqueNames = removeDuplicateNames(namesFromApi);
console.log('Original names:', namesFromApi);
console.log('Names with duplicates removed:', uniqueNames);
}
main();
The api data looks like this
{
objectId: '7igkvRJxTV',
Name: 'Aaron',
Gender: 'male',
createdAt: '2020-01-23T23:31:10.152Z',
updatedAt: '2020-01-23T23:31:10.152Z'
}
3
Answers
You try to use the
Set
over objects. If you want to filter duplicates byName
key you should:This way you remove an element from the array only if it has an another occurrence (by
Name
key as an ID) in the array (after its own index).class
Set
check value equality is based on the SameValueZero algorithm. You can remove dupulicated JavaScript object items like below:Set wouldn’t help you in case when name can be duplicate and other object fields can differ from each other.
The simpliest way is to use
uniqBy
function fromlodash
.This function creates a duplicate of given array, where duplicates with same property
Name
are removed. Order is guaranteed (would be left only first occurrence)юThis library simplifies common operations with collections / objects / etc.
Reference