My task involves retrieving a list of names from an API, but there are many duplicates that need to be filtered out. However, when I attempt to execute the removeDuplicateNames function, it simply 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 structure of the API data is as follows
{
objectId: '7igkvRJxTV',
Name: 'Aaron',
Gender: 'male',
createdAt: '2020-01-23T23:31:10.152Z',
updatedAt: '2020-01-23T23:31:10.152Z'
}