javascript - 如何在数组中找到最小值,不包括第一个索引
问题描述
我有一个值为 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 数组中除第一个索引之外的所有项目的最小值,但是我得到的只是整个数组的最小值。
解决方案
您可以对数组进行切片,Array#slice
并在没有第一个的情况下获取所有项目。
Math.min(...this.previousBalanceArray.slice(1))
推荐阅读
- jmeter - 如何在使用 jmeter 对任何应用程序进行负载测试时设置网络带宽
- javascript - module.exports 功能
- node.js - Azure Blob 存储 - 上传有进度的文件
- spring - Spring mvc Camel RouteBuilder rabbitMQ 连接重置异常
- odoo-11 - Odoo 的 qweb groups_id
- telegram - 如何使用 Telebot 库通过@link 获取组详细信息
- symfony - symfony 服务器启动 - 多次记录
- php - 用户注册表单 Wordpress 显示错误:“NONCE ERROR,PLEASE RELOAD”
- java - 删除尾随的“00”和斜杠
- angular - 如何在 Mat Table 中填充 FormArray?