javascript - 在像数组这样的对象中查找最小值和最大值
问题描述
我正在尝试从数组中找到最小值和最大值
尝试了各种方法,但找不到满足我需求的解决方案
我有一个数组对象
它的值为
{
0:exchange:bitfix
totaleth:334
1:exchange:coinbase
totaleth:6000
2:exchange:koinex
totaleth:3000
}
现在这个对象就像一个数组,我想根据 totaleth 字段显示最小值和最大值,任何 1 都有解决方案吗
我尝试了各种方法,例如 map reduce for loop,似乎没有任何效果
解决方案
假设一个对象数组,您可以通过取最小值和最大值来减少数组。
var array = [{ exchange: 'bitfix', totaleth: 334 }, { exchange: 'coinbase', totaleth: 6000 }, { exchange: 'koinex', totaleth: 3000 }],
{ min, max } = array.reduce(
(r, o, i) => i
? {
min: r.min.totaleth < o.totaleth ? r.min : o,
max: r.max.totaleth > o.totaleth ? r.max : o
}
: { min: o, max: o },
undefined
);
console.log('min', min);
console.log('max', max);
推荐阅读
- java - 输入数字然后取数字前后
- javascript - Sequelize 表 id 冲突
- oracle - dapper 和运行存储过程 ctx_ddl.sync_index
- ssms - SQL Server 管理 Studio 连接对话框授权下拉列表为空
- c++ - 仅使用按位运算反转数字
- java - WebElementProxy 应该实现什么才能将其传递给 Actions.moveToElement(WebElement)?
- jms - Apache Nifi,PublishJMS 处理器在将 json 消息发布到 IBM Websphere MQ 以覆盖 JMS_IBM_* 属性时失败
- windows - 如何为内部 Windows 网络中的 Web 服务器设置受信任的 SSL?
- javascript - 让 Redux 返回一个数组而不是嵌套对象
- ruby-on-rails - redis中rails缓存条目中的时间戳格式是什么?