function that accepts up to n ar

2018-03-21  本文已影响0人  pauli

Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

constary = (fn, n) => (...args) => fn(...args.slice(0, n));

Usage:

constfirstTwoMax = ary(Math.max,2);
[[2,6,'a'], [8,4,6], [10]].map(x => firstTwoMax(...x));        // [6, 8, 10]

From:

https://github.com/Chalarangelo/30-seconds-of-code
https://30secondsofcode.org/

上一篇 下一篇

猜你喜欢

热点阅读