user.date
меньше или равно report.report_date
. Но нужно отобразить только того пользователя, который будет ближе всего к значению в интервале. То есть как отобразить максимальную дату для user.date
, чтоб заработало условие. <div *ngFor="let report of reports">
<div *ngFor="let user of users">
<div *ngIf="report.users_id==user.user_id">
<div *ngIf="report.report_date >= user.date">
{{user.name}}
</div>
</div>
</div>
</div>
users: User[]
reports: Report[]
private loadUsers() {
this.usersService.fetch().subscribe(
users => {
this.users = users;
}
);
}
private loadReports() {
this.reportsService.fetch().subscribe(
reports => {
this.reports = reports
}
)
}
// шаблон
<div *ngFor="let user of reportsWithUser$|async">
{{user.name}}
</div>
//компонент
import {Observable} from "rxjs";
import {map, switchMap} from "rxjs/operators";
import * as _ from 'underscore';
interface User {
user_id: number;
name: string;
date: string;
}
interface Report {
report_date: string;
users_id: number;
}
interface UserWithReports extends User {
reports: Report[];
reportWithMaxDate: Report;
}
export function assignReportToUser(user: User, reports: Report[]): UserWithReports {
const repFiltered = reports.filter(report => report.report_date >= user.date);
return {...user, reports: repFiltered.filter(report => report.users_id === user.user_id), reportWithMaxDate: _.max(repFiltered, (r) => r.report_date)};
}
export class AnyComponent {
reportsWithUser$: Observable<UserWithReports[]>;
constructor(
public reportsService: any,
public usersService: any
) {
this.reportsWithUser$ = this.reportsService.fetch().pipe(
switchMap((reports: Report[]) => this.usersService.fetch().pipe(
map((users: User[]) => {
return users.map(user => assignReportToUser(user, reports)).filter((user) => user.reportWithMaxDate !== null);
})
)
)
)
}
}