热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

角2中是否有独特的过滤器?

如何解决《角2中是否有独特的过滤器?》经验,为你挑选了1个好方法。

我有一个json如下:

[{
  "_id": "58a58ea23744cd46918914ef",
  "entityId": "ce7200c1-3430-47ce-ab81-7d0622cb8cae",
  "name": "Portugal",
  "description": "Portugal",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58a8486ca4890027348a8966"
  },
  "image": {
    "_id": "58a8486ca4890027348a8965"
  },
  "metadata": {
    "primary": "pt",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "PT",
    "currency": "EUR",
    "_id": "58a8486ca4890027348a8964"
  }
}, {
  "_id": "58a58ea23744cd46918914f1",
  "entityId": "815b9e90-e36f-4488-ad50-6a259b6d034d",
  "name": "Qatar",
  "description": "Qatar",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58a8486ca4890027348a8969"
  },
  "image": {
    "_id": "58a8486ca4890027348a8968"
  },
  "metadata": {
    "primary": "qa",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "RO",
    "currency": "RO",
    "_id": "58a8486ca4890027348a8967"
  }
}, {
  "_id": "58a58ea23744cd46918914f2",
  "entityId": "e12ea227-c05c-42cf-b746-6bc537812a02",
  "name": "Romania",
  "description": "Romania",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58a8486ca4890027348a896c"
  },
  "image": {
    "_id": "58a8486ca4890027348a896b"
  },
  "metadata": {
    "primary": "ro",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "RO",
    "currency": "RON",
    "_id": "58a8486ca4890027348a896a"
  }
}, {
  "_id": "58a58ea23744cd46918914f4",
  "entityId": "8c22ccaf-f7b7-4009-9642-54580ad5ad4e",
  "name": "Russia",
  "description": "Russia",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58a8486ca4890027348a8972"
  },
  "image": {
    "_id": "58a8486ca4890027348a8971"
  },
  "metadata": {
    "primary": "ru",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "RU",
    "currency": "RUB",
    "_id": "58a8486ca4890027348a8970"
  }
}, {
  "_id": "58a58ea23744cd46918914fa",
  "entityId": "725b2011-177b-4fc5-8e1f-559b3e4e8958",
  "name": "United States",
  "description": "United States",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58a8486ca4890027348a8984"
  },
  "image": {
    "_id": "58a8486ca4890027348a8983"
  },
  "metadata": {
    "primary": "us",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "EN",
    "currency": "USD",
    "_id": "58 a8486ca4890027348a8982"
  }
}, {
  "_id": " 58 a58ea23744cd46918914d8",
  "entityId": "af884be9 - 067 d - 4 dd2 - 8646 - 5738 f5bb0efb",
  "name": "Germany",
  "description": "Germany",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58 a8486ca4890027348a89b7 "
  },
  "image": {
    "_id": "58 a8486ca4890027348a89b6 "
  },
  "metadata": {
    "primary": "de",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "DE",
    "currency": "EUR",
    "_id": "58 a8486ca4890027348a89b5 "
  }
}]

我将使用以下代码在下拉列表中显示这些值:

模板:


现在,如果你看到,元数据有重复的货币"EUR".是否有一个带有angular2的管道,这样我才能显示唯一的值metadata.currency



1> developer033..:

角2中是否有独特的过滤器?

不,您必须创建自定义管道才能执行此操作.

但是,我不认为创建pipe是非常必要的,您可以在组件中执行此操作.

您可以获得以下所有项目的唯一 currencies性:

export class AnyComponent {

  items: any[];
  currencies: string[];

  constructor() {
    // Populate your items... "items = blabla"

    // Get all currencies
    const curr = items.map(data => data.metadata.currency);

    // Unique currencies
    this.currencies = curr.filter((x, i, a) => x && a.indexOf(x) === i);
  }
}

然后,在您的模板中:

...


推荐阅读
author-avatar
傅雪莱_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有