Don’t want to spoil a great italian party but, maybe ditch the subscribe
, add async
pipe and also ditch one from
?
And the first sort can be done inside the main pipe using, map, right?
So something like this (with syntax errors probably):
<ion-content>
<ion-card *ngFor="let item of array_sorted | async">
<ion-card-header>
<ion-card-title>{{item[0]}}</ion-card-title>
</ion-card-header>
<ion-card-content>
<p *ngFor="let amount of item[1] ">
Amount: {{amount}}
</p> <br>
</ion-card-content>
</ion-card>
</ion-content>
import { Component } from '@angular/core';
import { groupBy, mergeMap, toArray } from 'rxjs/operators';
import { from, zip, of } from 'rxjs';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
array_sorted = Observable<any[]>;
array = [{
"data_filtro": "2022-05-17",
"amount": "8.35",
"tipo": "2",
"data_operation": "2022-05-17 20:13:51"
},
{
"data_filtro": "2022-05-11",
"amount": "33.35",
"tipo": "3",
"data_operation": "2022-05-16 13:03:51"
},
{
"data_filtro": "2022-05-17",
"amount": "11.35",
"tipo": "2",
"data_operation": "2022-05-17 13:03:51"
},
{
"data_filtro": "2022-05-16",
"amount": "33.35",
"tipo": "3",
"data_operation": "2022-05-16 13:03:51"
}
];
constructor() {
this.array_sorted= from(this.array)
.pipe(
map(array=>{array.sort((a, b) => a.data_filtro.localeCompare(b.data_filtro))}),
groupBy(
data => data.data_filtro,
item => item.amount
),
mergeMap(group => zip(of(group.key), group.pipe(toArray())))
);
}
}
And later on - remove this.array and replace by a service, and use $ sign for variable etc…for future forum posts