html - 如何在数组 angular 6 中找到最接近的较低值?
问题描述
我有一个对象数组,我需要根据角度 6 中的某个值或当前值从该数组中找到最接近的较低值,我已经尝试过,但它显示未定义。
app.component.html
<button (click) ="getPrev()">Previous Value</button>
app.component.ts
declare var require: any;
import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent{
arrayVal:any;
currentVal : any;
title = 'projectchart';
public array = [{"id":1},{"id":3},{"id":5}];
getPrev(){
this.currentVal = 5;
this.arrayVal= this.array;
let number = this.arrayVal.reverse().find(e => e <= this.currentVal);
console.log(number);
}
}
解决方案
你的数组中有 id 所以这个 -
let number = this.arrayVal.reverse().find(e => e.id <= this.currentVal);
不会是未定义的
推荐阅读
- python - Python - 在我的函数中编写就地选项[如何防止覆盖我的输入向量]
- python - 从 pandas 数据帧生成带有随机样本的输出文件
- java - 我的并发代码是同步的,但它没有同步打印值,但是每个线程都打印相同的值
- python - 如何导入文件夹中的所有脚本并将它们用作函数?
- javascript - 如何控制 HTML 页面在 GET 请求中返回的内容
- php - 对 Laravel Livewire 组件进行水合和脱水意味着什么?
- flutter - Flutter:如何更改 AppBar Title 字体?
- amazon-web-services - AWS Cloudformation:重用 Fn::FindInMap 的结果
- python - Pipenv 无法识别 PyEnv python 安装的垫片
- python - 如何修复此 python NameError?