0
variants: [{"vType":"red"},{"vType":"red"},{"vType":"black"}]

I need to use a filter to get unique colors as only "red and black" ill be using

<select class="form-control" [(ngModel)]="selectedVariant" (ngModelChange)="changeVariant(selectedVariant)" >
  <option  *ngFor="let c of selection">{{c.vType}}</option>
</select>

how to create a custom filter to filter unique colors.

1 Answers1

1

If you want to build custom filter you can see docs / tuto here

To get your unique collection you could use lodash _.uniqBy function or make a custom implementation using reduce like (from this StackOverflow post)

variants = [{"vType":"red"},{"vType":"red"},{"vType":"black"}];
uniqueVariants = variants.reduce(function(accumulatedVariants, variant) {
    if (!accumulatedVariants.find(v => v.vType === variant.vType)) {
          accumulatedVariants.push(c);
    }
    return accumulatedVariants;
}, []); 
Pierre Mallet
  • 7,053
  • 2
  • 19
  • 30
  • im not only looking (only) for my rep, but can you validate this answer so your question will no longer be marked as "unanswered", thx – Pierre Mallet Nov 24 '17 at 10:49