-
-
Save bugb/f86a32a838c995051df08c783f3fa984 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const collection = [ | |
{ attributeValue: ["RED", "GOLD"] }, | |
{ attributeValue: ["16G", "32G", "64G"] }, | |
{ attributeValue: ["PLUS"] } | |
] | |
function cartesian(data) { | |
const f = (a,b) => [].concat(...a.map(d => b.map(e => [].concat(d, e)))) | |
const recurse = (a, b, ...c) => (b? recurse(f(a, b), ...c): a) | |
return recurse(...data) | |
} | |
const values = collection.map(row => row.attributeValue) | |
console.log(cartesian(values).map(row => row.join(' / '))) | |
/* | |
[ 'RED / 16G / PLUS', | |
'RED / 32G / PLUS', | |
'RED / 64G / PLUS', | |
'GOLD / 16G / PLUS', | |
'GOLD / 32G / PLUS', | |
'GOLD / 64G / PLUS' ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment