2

I've got a Map<string, customObject> and I want to iterate over it using *ngFor.

I tried *ngFor="let mapItems of map | keyvalue" but this one sorts the keys in ascending order. I don't want the Map to be sorted at all. The values should be displayed in the original order.

Is there a way to achieve this?

MatterOfFact
  • 1,253
  • 2
  • 18
  • 49
  • 1
    https://stackoverflow.com/questions/52793944/angular-keyvalue-pipe-sort-properties-iterate-in-order – Phix Mar 27 '20 at 21:09
  • 2
    Does this answer your question? [angular keyvalue pipe sort properties / iterate in order](https://stackoverflow.com/questions/52793944/angular-keyvalue-pipe-sort-properties-iterate-in-order) – Yuriy Kravets Mar 27 '20 at 21:48
  • Thank you very much! This one https://stackoverflow.com/a/52794221/9888512 did it for me (originalOrder comparer) – MatterOfFact Mar 27 '20 at 21:49

1 Answers1

-1

You can write custom pipe.

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

and can use like

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

Sample:

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

xdeepakv
  • 7,835
  • 2
  • 22
  • 32