1

I have a two dropdown selects, one for country and one for nationality. Both dropdowns share an array: countryAndNationalityList.

I'm trying to only display the matching nationality once, as it is the same for each country.

<select class="form-control" formControlName="residence_country">
    <option *ngFor="let item of removeNationalityDuplicates(countryAndNationalityList.sort(sortCountryAsc))" [ngValue]="item.country">{{item.country}}</option>
</select>

<select class="form-control" formControlName="nationality">
    <option value="" disabled>Your Nationality?</option>
    <option *ngFor="let item of countryAndNationalityList.sort(sortNationalityAsc)" [ngValue]="item.nationality">{{item.nationality}}</option>
</select>

My countryAndNationalityList array looks like this:

[{country: "United Arab Emirates Abu Dhabi", nationality: "Emirati"},
{country: "United Arab Emirates Ajman", nationality: "Emirati"},
{country: "United Arab Emirates Al-Quwain", nationality: "Emirati"},
{country: "United Arab Emirates Dubai", nationality: "Emirati"},
{country: "United Arab Emirates Fujairah", nationality: "Emirati"},
{country: "United Arab Emirates Ras Al Khaimah", nationality: "Emirati"},
{country: "United Arab Emirates Sharjah", nationality: "Emirati"}]

I am trying to remove the duplicate Emirati in the Nationality dropdown using a function: removeNationalityDuplicates that remove duplicates like this:

<select class="form-control" formControlName="nationality">
    <option value="" disabled>Your Nationality?</option>
    <option *ngFor="let item of removeNationalityDuplicates(countryAndNationalityList.sort(sortNationalityAsc))" [ngValue]="item.nationality">{{item.nationality}}</option>
</select>

where removeNationalityDuplicates is

  removeNationalityDuplicates(array) {
    return [... new Set(array)];
  }

  sortNationalityAsc(previous, next) {
    return previous.nationality > next.nationality;
  }

I have also tried using:

removeNationalityDuplicates(array) {
  return array.reduce((uniqueArr, curVal)=> {
    if (!uniqueArr.includes(curVal.nationality)) {
      uniqueArr.push(curVal);
    }
    return uniqueArr;
  }, []);
}

How do remove the duplicate nationalities?

2 Answers 2

2
removeNationalityDuplicates(array) {
  return Array.from(new Set(array.map(x => x.nationality)));
}

Explanation:

  1. array.map(x => x.nationality) create a new array with only the nationality key
  2. new Set(array.map(x => x.nationality)) create a set of this array ( works like distinct )
  3. Array.from(new Set(array.map(x => x.nationality))); convert the set to an array.
Sign up to request clarification or add additional context in comments.

Comments

1

If you are searching for a possiblity to filter the original array but without changing its properties, use this solution:

const removeNationalityDuplicates = (arr) => [...new Map(arr.map(item => [item.nationality, item])).values()]


const list = [{
    country: "United Arab Emirates Abu Dhabi",
    nationality: "Emirati"
  },
  {
    country: "United Arab Emirates Ajman",
    nationality: "Emirati"
  },
  {
    country: "United Arab Emirates Al-Quwain",
    nationality: "Emirati"
  },
  {
    country: "United Arab Emirates Dubai",
    nationality: "Emirati"
  },
  {
    country: "United Arab Emirates Fujairah",
    nationality: "Emirati"
  },
  {
    country: "United Arab Emirates Ras Al Khaimah",
    nationality: "Emirati"
  },
  {
    country: "United Arab Emirates Sharjah",
    nationality: "Emirati"
  }
]

console.log(removeNationalityDuplicates(list))

If you'd wish to have only the list of nationalities, look at enno.void's solution.

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.