skip to Main Content

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


  1. You try to use the Set over objects. If you want to filter duplicates by Name key you should:

    function removeDuplicateNames(names) {
        const namesOnly = names.map((name) => name.Name);
    
        return names.filter((name, index) => !namesOnly.includes(name.Name, index+1));
    }
    

    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).

    Login or Signup to reply.
  2. class Set check value equality is based on the SameValueZero algorithm. You can remove dupulicated JavaScript object items like below:

    interface User {
      objectid: string;
      name: string;
    };  
    function removeDuplicateNames(users: User[]): User[] {
      const record: { [string]: boolean } = {};
      const result: User[] = [];
      for (const item of users) {
        if (!record[item.name]) {
          result.push(item);
          recrod[item.name] = true;
        }
      }
      return result;
    }
    
    Login or Signup to reply.
  3. 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 from lodash.

    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

    import { uniqBy } from  'lodash';
    
    // your code
    
    function removeDuplicateNames(names) {
        return uniqBy(data, 'Name');
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search