美文网首页
function that accepts up to n ar

function that accepts up to n ar

作者: pauli | 来源:发表于2018-03-21 16:20 被阅读0次

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/

相关文章

网友评论

      本文标题:function that accepts up to n ar

      本文链接:https://www.haomeiwen.com/subject/tnsetttx.html