【问题标题】:How to find the maximum value?如何找到最大值?
【发布时间】:2019-05-23 08:59:25
【问题描述】:

需要根据c.date <= this.selectedReport.report_date这个条件找到最合适的日期(显示一个值)。但是这部分条件if (Math.max(...this.costs.map(c => c.date))){} 不起作用,因此根本不显示数据。我怎样才能解决这个问题?没有任何错误。

reports: Reports[]
income: Income[]
costs: Costs[]
selectedReport = null
filteredIncome = []
filteredСosts = []

onSelectedReport(reportId) {
  this.selectedReport = this.reports.find(
    el => {
      return el.report_id === reportId
    }
  )
  if (this.incomeService) {
    this.incomeService.fetchAll().subscribe(
      income => {
        this.income = income
        this.filteredIncome = this.income.filter(
          (income) => income.income_id == this.selectedReport.r_income_id
        )
        if (this.costsService) {
          this.costsService.fetch().subscribe(
            costs => {
              this.costs = costs
              for(let i of this.filteredIncome){
                for(let c of costs){
                  if(Math.max(...this.costs.map(c => c.date))) {
                    if(c.costs_id==i.i_costs_id){
                      if (c.date <= this.selectedReport.report_date) {
                        this.filteredСosts.push(c)
                      }
                    }
                  }
                }
              }
            }
          )
        }
      }
    )
  }
}

尝试使用库 ngx-pipes。并从循环中删除这一行 if (Math.max(...this.costs.map(c =&gt; c.date))){} 替换它:

<div *ngFor="let report of reports" class="center " (click)="onSelectedReport(report.report_id)">
  <a>{{report.report_date | date: 'dd.MM.yyyy'}}</a>
</div>
<div  *ngIf="selectedReport">
  <div *ngFor="let i of filteredIncome">
    <div *ngFor="let c of filteredСosts | max: 'date'">
      {{c.name}}
    </div>
  </div>
</div>

我收到此错误:

错误错误:找不到不同的支持对象“-Infinity” 输入“数字”。 NgFor 仅支持绑定到 Iterables,例如 数组。

atNgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3152)

在 checkAndUpdateDirectiveInline (core.js:9246)

在 checkAndUpdateNodeInline (core.js:10507)

在 checkAndUpdateNode (core.js:10469)

在 debugCheckAndUpdateNode (core.js:11102)

在 debugCheckDirectivesFn (core.js:11062)

在 Object.eval [as updateDirectives] (RepHoursComponent.html:48)

在 Object.debugUpdateDirectives [as updateDirectives] (core.js:11054)

在 checkAndUpdateView (core.js:10451)

在 callViewAction (core.js:10692)

【问题讨论】:

  • 实际上,您在错误的地方检查 Math.max,您应该在地图中检查它。
  • @Hp_issei 有些东西我不太明白,你是这个意思吗? if(this.costs.map(Math.max( c.date))) {}

标签: angular typescript


【解决方案1】:

如果 c.date 是 Date JavaScript 对象,试试这个 c.date.getTime()

供参考Date.prototype.getTime()

【讨论】:

    【解决方案2】:

    删除 math.max 检查并应用如下。

    (...this.costs.map(c => c.date)) { if(Math.max(c => c.date)){ } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多