我有一个对象数组,我需要根据角度 6 中的某个值或当前值从该数组中找到最接近的较低值,我已经尝试过,但它显示未定义。
<button (click) ="getPrev()">Previous Value</button>
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);
}
}
您可以找到最近的壁橱号码,如下所示
let arr = [
{ "id" : 1 },
{ "id" : 3 },
{ "id" : 5 }
];
let referenceNumber = 4;
let nearestLowest = arr.reduce((acc, val) => {
if(val['id'] < referenceNumber && val['id'] > acc)
acc = val['id'];
return acc;
}, Math.max());
console.log(nearestLowest);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句