0

Below are the duplicated fields i want to remove/filter.

Dropdown Name -> Family Values displaying currently ->

  1. BU
  2. State BU
  3. State BU
  4. BU

I want to remove these duplicate values from the dropdown.

 ngOnInit() {
     if (isUser == "Yes")
    {
     this.UserFamily.forEach(val => { 
       let found = this.deviceFamily.find(x => x == val.familyName);
       if (found == null) {
         this.deviceFamily.push(val.familyName);
       }
     });
    }
}

Plese help me with how can i remove the duplicates.

S.Sehgal
  • 25
  • 2
  • 9
  • Do you wish to remove duplicate values from an array? – ruth May 11 '22 at 09:17
  • @ruth yes, want to remove duplicates – S.Sehgal May 11 '22 at 09:18
  • @ruth its like, In the above code, in "UserFamily" array, we get the family. and in the "deviceFamily", we also get the family for different type of user. So currently the issue is i am getting the families from both these arrays and that's why duplicate values are displaying in the dropdown. – S.Sehgal May 11 '22 at 09:20
  • Please refer the linked post. – ruth May 11 '22 at 09:20
  • this.deviceFamily = [...new Set(this.deviceFamily.map(cal => val. familyName))] – Ashot Aleqsanyan May 11 '22 at 09:22
  • @AshotAleqsanyan Unable to understand, could you please eleborate the code? – S.Sehgal May 11 '22 at 09:24
  • Hello @S.Sehgal, sure. we are generating a new array with the family names after we are creating new `Set` Colection, Set is ignoring duplicate values, but as we need an array instead of collection we are converting the collection with unique values into Array by `...`(Spread operator) – Ashot Aleqsanyan May 11 '22 at 09:28
  • Lets Simplify little bit: `const onlyFamilyNamesList = this.deviceFamily.map(cal => val. familyName);` - we are generating new array by family names of each item `const uniqueCollectionOfFamilyNames = new Set(onlyFamilyNamesList)` we are generation new Set that is ignoring duplicate values. And After we will need to convert the collection into Array by `const uniqueArrayOfFamilyNames = [...uniqueCollectionOfFamilyNames]` – Ashot Aleqsanyan May 11 '22 at 09:32
  • @AshotAleqsanyan: Do you wish to post a specific answer? I could reopen the question. – ruth May 11 '22 at 09:45
  • 1
    No @ruth, it's ok, I left a explanation comment – Ashot Aleqsanyan May 11 '22 at 10:41

0 Answers0