javascript - 跳过 NULL 并在下拉列表中仅显示唯一值
问题描述
我正在尝试从 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);
}
在 for 循环中获取唯一值,当我尝试删除 NULL 并将 if 条件更改为
if( (flags[this.ShipmentList[i].customer_shipto_name] || this.ShipmentList[i].customer_shipto_name)) continue;
对于上面的代码,它不返回任何数据
解决方案
您可以使用以下方法删除 NULL 值:
this.shipTo = this.ShipmentList.filter(_ => _.customer_shipto_name);
然后使用 new Set() 仅获取唯一值:
const uniqueValues = new Set(this.shipTo);
推荐阅读
- ios - 如何在ios中检查联系人是否是谷歌联系人?
- java - 想要代码从 cmd 行读取和应用信息
- r - 在 ggplot 中更改地图的调色板
- typescript - 仅对对象的已定义成员强制类型,而不将“keysof typeof [object]”扩大到 string[]?
- amazon-web-services - 原始实例已终止,无法通过 EBS 快照恢复卷 RDP 进入新实例
- javascript - 如何显示实时倒计时然后关闭窗口?
- node.js - Firebase CLI Typescript 错误 TS2345 在之前编译的代码上引发
- reactjs - 我正在尝试安装 react-native-image-picker 但收到此错误
- c++ - make_heap 和 pop_heap 可以,但 push_heap 不行
- javascript - 如何获取不和谐频道的创建者?