【问题标题】:Ho to display unique values in the angular material drop down?何在角度材料下拉列表中显示唯一值?
【发布时间】:2019-10-19 14:29:17
【问题描述】:

我正在尝试使用如下所示的角度材料来显示选择的选项。

其中 appitem 是数组列表。

<mat-form-field>
    <mat-select placeholder="Select app" formControlName="appControl" required> 
      <mat-option *ngFor="let app of appitem" [value]="app.appname">
                {{app.appname}}
      </mat-option>
    </mat-select>
</mat-form-field>

但这里 app.appname 包含重复值,我只想显示唯一值。

你能帮忙吗?

【问题讨论】:

    标签: angular typescript angular-material angular7


    【解决方案1】:

    好吧,您可以像这样过滤出您的数组以使其独一无二,而不是放入材料 -

    var myArray = ['5', 1, '6', 8, '1'];
    var unique = myArray.filter((v, i, a) => a.indexOf(v) === i); 
    
    console.log(unique);

    独一无二的地方

    ['5', 1, 6, '8']
    

    【讨论】:

    • 您的示例是错误的,myArray 已经是唯一的(1 和 '1' 不同),此 sn-p 的输出将是:["5",1,"6",8 ,"1"]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2019-05-13
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多