Typescript 如何使用“选择”选项筛选对象

Typescript 如何使用“选择”选项筛选对象,typescript,object,Typescript,Object,我有一个对象,代码中有一些枚举值。当我尝试使用选择选项过滤它时。下面是一个片段: export enum BranchRegion { A = "A", B = "B", C = "C", R = "R" } 和我的ts文件: branchRegion = Object.keys(BranchRegion); 和html: <select class="form-control" name="region" formControlName="region"

我有一个对象,代码中有一些枚举值。当我尝试使用选择选项过滤它时。下面是一个片段:

export enum BranchRegion {
  A = "A",
  B = "B",
  C = "C",
  R = "R"
}
和我的ts文件:

branchRegion = Object.keys(BranchRegion);
和html:

<select
  class="form-control"
  name="region"
  formControlName="region"
  id="field_region"
  [class.is-invalid]="
    branchForm.get('region').invalid &&
    (branchForm.get('region').dirty ||
      branchForm.get('region').touched)
  "
  (change)="filterLocations($event.target.value)"
>
  <option *ngFor="let item of branchRegion" [value]="item">{{
    branchRegionLabel[item]
  }}</option>
</select>
结果:


我做错了什么?有什么建议吗?

filterLocations
方法中,很难看到您想要做什么。
this.branchRegion
已经是一个数组。所以在上面执行
Object.keys()
有点过头了:)。也许你想做这样的事?通过在源对象上执行
Object.keys
,可以确保不在已过滤的数组上进行过滤

filterLocations(value: any) {
  this.branchRegion = Object.keys(BranchRegion).filter((region) => region === value)
}

您得到的例外情况如下:

Object.keys(this.branchRegion).every(value)

Array.each
-each()方法测试数组中的所有元素是否通过所提供函数实现的测试。它返回一个布尔值

您没有为每个用户提供功能。

这是您的问题:

Object.keys(this.branchRegion).every(value);
每个函数都需要一个回调函数,而您给了它一个值。如果只想筛选选定的项目,请使用筛选方法:

Object.keys(this.branchRegion).filter(v => v === value);
Object.keys(this.branchRegion).filter(v => v === value);