Skip to content

Instantly share code, notes, and snippets.

@tapmodo
Last active November 21, 2018 15:05
Show Gist options
  • Save tapmodo/f284381a80da4adf396bc6a68d52d8cf to your computer and use it in GitHub Desktop.
Save tapmodo/f284381a80da4adf396bc6a68d52d8cf 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