Skip to content

Instantly share code, notes, and snippets.

@bugb
Forked from tapmodo/cartesian.js
Created November 20, 2018 10:57
Show Gist options
  • Save bugb/f86a32a838c995051df08c783f3fa984 to your computer and use it in GitHub Desktop.
Save bugb/f86a32a838c995051df08c783f3fa984 to your computer and use it in GitHub Desktop.
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