【问题标题】:Angular: Is there a way to iterate over Map<,> without sorting? [duplicate]Angular:有没有办法在不排序的情况下迭代 Map<,>? [复制]
【发布时间】:2020-03-27 21:04:57
【问题描述】:

我有一个 Map&lt;string, customObject&gt;,我想使用 *ngFor 对其进行迭代。

我尝试了*ngFor="let mapItems of map | keyvalue",但这一次按升序对键进行排序。我根本不希望对地图进行排序。值应按原始顺序显示。

有没有办法做到这一点?

【问题讨论】:

标签: angular typescript angular9


【解决方案1】:

您可以编写自定义管道。

@Pipe({
  name: "appmap"
})
export class AppMap implements PipeTransform {
  transform(m: Map<string, object>): any[] {
    console.log([...m]);
    return [...m];
  }
}

可以像这样使用

<div *ngFor="let item of map | appmap">
    {{item[0]}}:{{item[1] |json }}
  </div>

示例:

https://codesandbox.io/s/heuristic-ellis-hmw2p

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2019-07-23
    • 2019-12-30
    • 2021-01-10
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    相关资源
    最近更新 更多