xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

how to find max value of array in js All In One

how to find max value of array in js All In One

Math.max

const data = ["37.02","15.75","11.22","7.88","6.50","4.83","3.45","2.56","1.93","1.51","1.20","0.95","0.79","0.64","0.54","0.42","0.35","0.32","0.29","43.17"];
// (20) ["37.02", "15.75", "11.22", "7.88", "6.50", "4.83", "3.45", "2.56", "1.93", "1.51", "1.20", "0.95", "0.79", "0.64", "0.54", "0.42", "0.35", "0.32", "0.29", "43.17"]

Math.max(data.map(item => (Math.ceil(parseInt(item) / 10) * 10)));
// NaN
data.map(item => (Math.ceil(parseInt(item) / 10) * 10));
// (20) [40, 20, 20, 10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 50]

Math.max(...data.map(item => (Math.ceil(parseInt(item) / 10) * 10)));
// 50

reduce



array & max & min

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};

refs

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-07-20 16:18  xgqfrms  阅读(47)  评论(1编辑  收藏  举报