0

I m trying to display data from array inside a dropdown using ngFor. But i am getting duplicate values.How do i remove it?

Here is the entire data: https://restcountries.eu/rest/v2/all

Here is the currencies array that i am trying to loop..

enter image description here

I m trying to display unique values based on name field of currency array.

Here is the GitHub link to the code:

https://github.com/saisreereddy/RestfulCountriesv3

The logic is inside the region component.

Any kind of help is highly appreciated.

5
  • Have you seen this stackoverflow.com/questions/43512528/…? Commented May 16, 2018 at 16:57
  • does the data currencies may have same names? Commented May 16, 2018 at 16:57
  • yes they have same names in some arrays Commented May 16, 2018 at 17:00
  • @prachi...I have seen it and also tried but not working Commented May 16, 2018 at 17:00
  • What you want to do is create a custom pipe to filter out duplicate data. There are a number of SO posts on this. @Prachi listed one. Here are some more stackoverflow.com/questions/38362047/… stackoverflow.com/questions/34417250/… Commented May 16, 2018 at 17:01

1 Answer 1

0

If you're not comfortable making a custom pipe, this can be done easily programatically, run this function on your currency array:

filterData(yourCurrencyData: any[]): any[] {
  const filteredArray = [];
  for (const currency of yourCurrencyData) {
    if(!filteredArray.find(c => c.code === currency.code) {
      filteredArray.push(currency);
    }
  }
  return filteredArray;
}

This will return your filtered array. Not terribly efficient, and a map would have faster look ups, but since there are a small number of currencies this is not an issue as this is very readable and the difference would not be noticeable by humans with only a few hundred records. Feel free to replace any with a Data Type. And remember to *ngFor over the new filtered array.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.