5

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>}>.
1

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Does not work if you have "target: es5" in your tsconfig.json. Must be ES6. Thanks a bunch!
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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.