2

So I found an API which I'm fetching all the countries and their respective cities. This is the API: https://documenter.getpostman.com/view/1134062/T1LJjU52#4829d16f-0f4e-43ec-886e-68ebad1221d8

I'm getting duplicates of cities back from the response, which I've checked in Postman, as can be seen below:

     {
        "country": "United States",
        "cities": [
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbotsford",
       ],
      },

I need to remove the duplicates, and I've managed to get it to work, but it's not too pretty... This is my function for formatting the code:

    getCountries = async () => {
    if(!this.mounted) return;

    interface typeData {
        data: Array<{country: string, cities: Array<string>}>;
        error: boolean;
        msg: string;
    }
    const result: typeData = await this.data.GetCountries();

    let findDuplicates = result.data.map(i => {
        let currentCountry = i.country;
        let currentCities: Array<string> = [];
        i.cities.filter(c => {
            if(!currentCities.includes(c)) currentCities.push(c);
        });
        let finalArray: Array<{country: string, cities: Array<string>}> = [{
            country: currentCountry,
            cities: currentCities
        }];
        return finalArray;
    }).reduce((sofar, current) => [...sofar, ...current], []);
    findDuplicates && this.setState({data: {weather: this.state.data?.weather, countries: findDuplicates}})
}

This feels like a not so efficient way to go about this, is there any way I can do this in a single line of code, with reduce, map or filter?

I've found similar examples on here, but none of them has this structure:

Array<{country: string, cities: Array<string>}>.
Martin
  • 47
  • 1
  • 5
  • Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – A_A Jun 27 '21 at 13:09

3 Answers3

3

You can reduce duplicates using Set:

let response = {
  "country": "United States",
  "cities": [
    "Abbeville",
    "Abbeville",
    "Abbeville",
    "Abbeville",
    "Abbeville",
    "Abbotsford",
  ],
};

response.cities = [...new Set(response.cities)];

console.log(response.cities);
      
      
Shravan Dhar
  • 1,455
  • 10
  • 18
  • 1
    This worked! Does not work if you have "target: es5" in your tsconfig.json. Must be ES6. Thanks a bunch! – Martin Jun 27 '21 at 14:12
0

Similar question: Remove duplicate values from JS array

or you may try this

 let data={
        "country": "United States",
        "cities": [
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbotsford",
       ],
      }
      let obj={}
      data.cities.forEach((x)=>obj[x]=x)
      let newArr=Object.keys(obj)
      console.log(newArr)
nazlikrmn
  • 59
  • 5
0

you can use a combination of Array.filter and Array.findIndex to do that. Its not the most efficient way of removing duplicate.

const data = [
    {
        "country": "United States",
        "cities": [
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbotsford",
       ],
    },
    {
        "country": "United States",
        "cities": [
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbeville",
            "Abbotsford",
       ],
    }
]

type IsDuplicate<T> = (a: T, b: T) => boolean

const removeDuplicate = <TItems> (collection: TItems[], isDuplicate: IsDuplicate<TItems>) => 
    collection.filter((item, index, items: TItems[]) => 
        items.findIndex(secondItem => isDuplicate(item, secondItem)) === index)

console.log(removeDuplicate(data, (a, b) => a.country === b.country))
botf
  • 140
  • 1
  • 12