創(chuàng)建一個元素數(shù)組,將zip生成的數(shù)組中的元素解構(gòu)并應(yīng)用提供的函數(shù)矾兜。
使用Math.max()和 ( ...) 展開運算符獲取數(shù)組中最長的子數(shù)組,通過Array.prototype.map()方法使每個元素成為一個數(shù)組。
使用Array.prototype.reduce()和Array.prototype.forEach()方法將分組值映射到單個數(shù)組案站。
使用Array.prototype.map()和展開運算符 (...) 應(yīng)用于fn每個單獨的元素組继准。
const unzipWith = (arr, fn) =>
? arr
? ? .reduce(
? ? ? (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
? ? ? Array.from({
? ? ? ? length: Math.max(...arr.map(x => x.length))
? ? ? }).map(x => [])
? ? )
? ? .map(val => fn(...val));
更多內(nèi)容請訪問我的網(wǎng)站 www.icoderoad.com