LoginSignup
14
12

More than 5 years have passed since last update.

Math.maxで配列中の最大値を取得する

Posted at

普通のJSで配列の最大値を取る場合ってどうします?Math.maxは普通2個の引数を取って大きい方を返しますが、実は何個でも引数を取れます。これを応用してMath.maxをapplyすると適当な配列の最大値を返せます。

Math.max.apply(null, [1,4,8,7]); //=> 8

jquery.masonry.jsで使われてます。
https://github.com/desandro/masonry/blob/master/jquery.masonry.js#L177

最近はunderscore.js等でmaxなどを使うかもしれませんが、備忘録として。

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12