【问题标题】:How to get unique elements using pipe in angular 4如何在角度 4 中使用管道获取独特元素
【发布时间】:2018-11-26 13:42:20
【问题描述】:

我的 facilityListArray 如下所示,里面有 ClinicServices 数组,我想只过滤来自这个 ClinicServices 的唯一元素(获取唯一的 parentName 对象)数组列表。下面是facilityListArray 的屏幕截图。在附图中,我显示了第一个数组列表的扩展列表,其中包含诊所服务列表。同样,在第二个列表中,我有一个 ClinicServices 列表,其中包含第一个列表的重复项。所以我只想从整个数组列表中获取唯一的 clincServices。

下面是我的管道文件:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'removeduplicate'
})
export class RemoveduplicatePipe implements PipeTransform {

  transform(value: any, args?: any): any {
    // Remove the duplicate elements
    const art = value.map( x => {
        return x.clinicServices.map(y => {
            return y.value;
        });
    }).reduce((acc, ele, i) => {
        acc = acc.concat(ele);
        return acc;
    });
    return new Set(art);
}
}

使用管道的 HTML 代码:

 <div class="col-sm-12 required" *ngFor="let facilitySearchResult of facilityListArray | removeduplicate">

                                    <label class="container">
                                      <input type="checkbox">{{facilitySearchResult}}
                                      <span class="checkmark"></span>
                                    </label>
                                  </div>

没有管道的HTML代码:

如果我在没有管道的情况下使用下面的代码,如果 ClinicService 正确但有重复的元素,我会得到列表。所以我实现了上面的管道代码并在我的 HTML 中使用了它,但它不起作用。我得到上面代码的空列表。非常感谢任何帮助。

 <div class="col-sm-12 required" *ngFor="let facilitySearchResult of facilityListArray; let i=index">
                                <div class="col-sm-12 required" *ngFor="let facilityServiceResult of facilitySearchResult.clinicServices; let j=index">
                                  <label class="container">
                                    <input type="checkbox">{{facilityServiceResult.parentName}}
                                    <span class="checkmark"></span>
                                  </label>
                                </div>
                              </div>

【问题讨论】:

标签: angular filter pipe unique


【解决方案1】:

Pipe 中的一个小错误

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'removeduplicate'
})
export class RemoveduplicatePipe implements PipeTransform {

    transform(value: any, args?: any): any {
        // Remove the duplicate elements
        const art = value.map( x => {
            return x.clinicServices?x.clinicServices.map(y => {
                return y.parentName?y.parentName:null; //<--parentName instead of value
            }):[];
        }).reduce((acc, ele, i) => {
            acc = acc.concat(ele);
            return acc;
        }).filter(z=>{ //<--to filter out null parentNames
            if(z){
                return z;
            }
        });
        return new Set(art);
    }
}

你应该可以得到clinicServices中唯一的parentName

【讨论】:

  • 我试图更改为 parentName ,但它没有列出 parentName。我已附上图片供您参考。
  • 您遇到的错误是什么?您上传的图像仅显示您设置断点的行。
  • ERROR TypeError: Cannot read property 'map' of null at removeduplicate.pipe.ts:11 另外我没有在我的应用程序中获得任何父名称列表
  • 很可能clinicServices 为空或parentName 丢失,因此出现错误。需要处理错误。我已经编辑了代码。
  • 太棒了!!该解决方案现在效果很好! ..非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多