碎片時(shí)間學(xué)編程「273]:數(shù)組的叉積--從提供的兩個(gè)數(shù)組中創(chuàng)建一個(gè)新數(shù)組

數(shù)組的叉積--從提供的兩個(gè)數(shù)組中創(chuàng)建一個(gè)新數(shù)組
通過(guò)從數(shù)組中創(chuàng)建每個(gè)可能的對(duì),從提供的兩個(gè)數(shù)組中創(chuàng)建一個(gè)新數(shù)組。
使用 Array.prototype.reduce(),Array.prototype.map() 和 Array.prototype.concat() 方法從兩個(gè)數(shù)組的元素中生成所有可能的對(duì)。
JavaScript
const xProd = (a, b) =>
?a.reduce((acc, x) => acc.concat(b.map(y => [x, y])), []);
示例
xProd([1, 2], ['a', 'b']); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
更多內(nèi)容請(qǐng)?jiān)L問(wèn)我的網(wǎng)站:https://www.icoderoad.com
標(biāo)簽: