跳过NULL并在下拉列表中仅显示唯一值

时间:2019-08-19 14:45:25

标签: javascript angular typescript angular7

我试图从Angular应用程序调用Web API,并在两个下拉列表中显示结果。我有下面的代码,我试图在下拉列表中仅显示唯一值,而没有任何NULL

 service.get<ShipmentByProject[]>(this.entityUrl).subscribe(x => {this.ShipmentList = x});

  for(let i=0; i< this.ShipmentList.length; i++) {
    if( flags[this.ShipmentList[i].customer_shipto_name]) continue;
    flags[this.ShipmentList[i].customer_shipto_name] = true;
    this.shipTo.push(this.ShipmentList[i].customer_shipto_name);
  }

enter image description here

在for循环中具有用于获取唯一值的功能,当我尝试删除NULL并将if条件更改为

时,它可以工作
   if( (flags[this.ShipmentList[i].customer_shipto_name] || this.ShipmentList[i].customer_shipto_name)) continue;

对于上面的代码,它不返回任何数据

1 个答案:

答案 0 :(得分:2)

您可以使用以下方法删除NULL值:

this.shipTo = this.ShipmentList.filter(_ => _.customer_shipto_name);

,然后使用新的Set()仅获取唯一值:

const uniqueValues = new Set(this.shipTo);