首页 > 解决方案 > 如何在数组中找到最小值,不包括第一个索引

问题描述

我有一个值为 0 的数组。每当用户输入与存款变量相关的数字时,customerAccount 变量就会更新,然后将该 customerAccount 变量推送到数组的第一个索引。我现在想检查客户余额是否小于数组中的所有值,第一个索引除外。

我尝试了以下方法:

deposit: number;
withdrawal: number;
customerAccount=0;
previousBalance=[0];


calculateDeposits(){

// User inputs amount and the deposit updates the currentAccount 
this.customerAccount += this.deposit;

// The customer Account is then placed to the first index of the array 
this.previousBalance.unshift(this.customerAccount);


// This is where it goes wrong:

if (this.customerAccount >= Math.min(...this.previousBalanceArray)) {

// Run Some Code
        }


}



我只想要 previousBalance 数组中除第一个索引之外的所有项目的最小值,但是我得到的只是整个数组的最小值。

标签: javascriptarraystypescriptionic-frameworkionic4

解决方案


您可以对数组进行切片,Array#slice并在没有第一个的情况下获取所有项目。

Math.min(...this.previousBalanceArray.slice(1))

推荐阅读