Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 角度2过滤掉重复出现的下拉列表_Arrays_Angular_Typescript_Ionic2_Angularjs Filter - Fatal编程技术网

Arrays 角度2过滤掉重复出现的下拉列表

Arrays 角度2过滤掉重复出现的下拉列表,arrays,angular,typescript,ionic2,angularjs-filter,Arrays,Angular,Typescript,Ionic2,Angularjs Filter,有人知道如何过滤am angular 2下拉列表中的报告结果吗。我目前正试图在模板*ngFor中执行此操作,但运气不佳。我也要试试定制的烟斗。数据来自JSON数组。在下面的对象中,我试图只显示“国有实体”的一个实例 我的数据对象 items[ { "currentBUName":"Financial Services" } { "currentBUName":"State Owned Entities" }

有人知道如何过滤am angular 2下拉列表中的报告结果吗。我目前正试图在模板*ngFor中执行此操作,但运气不佳。我也要试试定制的烟斗。数据来自JSON数组。在下面的对象中,我试图只显示“国有实体”的一个实例

我的数据对象

items[
      {  
       "currentBUName":"Financial Services"
      }
      {  
       "currentBUName":"State Owned Entities"
      }
      {  
       "currentBUName":"State Owned Entities"
      }
     ]
我的ts代码摘录

    <ion-item>
     <ion-label>Please select current business unit</ion-label>
      <ion-select [(ngModel)]="selectedValue">
          <ion-option *ngFor="let indexx of this.items;"[value]="indexx">{{indexx.currentBUName}}</ion-option>
      </ion-select>
    </ion-item>

请选择当前业务单元
{{indexx.currentBUName}

设置
项的值时
使用筛选功能仅获取唯一值。可以这样做:

this.items=this.items.filter((v,i,arr)=>arr.indexOf(v)==i);
注意:如果此数据来自服务器,则在服务器端进行此过滤以减少通过网络传输的重复信息量将更有意义。

为什么不使用阵列


然后改用
uniqueItems

其他答案也不错,但一种方法是用一些更通用的实用函数来定义它

function groupBy<T>(values: T[], keySelector: (value: T) => string): {[key: string]: T[]} {
  return values
      .map(value => ({ key: keySelector(value), value }))
      .reduce((groups, { key, value }) => ({
        ...groups,
        [key]: groups[key] && groups[key].concat(value) || [value]
      }), {});
}

function distinctBy<T>(values: T[], keySelector: (value: T) => string): T[] {
  const groups = groupBy(values, keySelector);
  return Object.values(groups).map(([representative]) => representative);
}

感谢您的回复,但由于Yoav Schniederman的链接
transform(){if(this.items!==undefined&&this.items!==null){console.log(uqby(this.items,'currentBUName'));this.currentBUvals=u.uniqBy(this.items,'currentBUName');return u.uniqBy(this.items,'currentBUName');}返回this.items;}
function groupBy<T>(values: T[], keySelector: (value: T) => string): {[key: string]: T[]} {
  return values
      .map(value => ({ key: keySelector(value), value }))
      .reduce((groups, { key, value }) => ({
        ...groups,
        [key]: groups[key] && groups[key].concat(value) || [value]
      }), {});
}

function distinctBy<T>(values: T[], keySelector: (value: T) => string): T[] {
  const groups = groupBy(values, keySelector);
  return Object.values(groups).map(([representative]) => representative);
}
this.items = distinctBy(incomingItems, item => item.currentBUName);