Last active
September 25, 2021 12:05
-
-
Save perjo927/2db7dc0231f8721b89326da5a520cfcf to your computer and use it in GitHub Desktop.
Combine
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 categorizeValues = ([category, values]) => | |
values.map(value => Object.fromEntries([[category, value]])) | |
const combineCategories = (prev, curr) => | |
curr.flatMap(currEl => prev.map(prevEl => ({...prevEl, ...currEl}))) | |
const combinations = input => | |
Object.entries(input).map(categorizeValues).reduce(combineCategories); | |
/* | |
input | |
{ code: ["2xx", "3xx"], source: ["cdn", "internal"] } | |
output | |
[ | |
{ code: '2xx', source: 'cdn' }, | |
{ code: '3xx', source: 'cdn' }, | |
{ code: '2xx', source: 'internal' }, | |
{ code: '3xx', source: 'internal' } | |
] | |
input | |
{ code: ["2xx", "3xx"], source: ["cdn", "internal"], colors: ["blue", "red"] } | |
output | |
[ | |
{ code: '2xx', source: 'cdn', colors: 'blue' }, | |
{ code: '3xx', source: 'cdn', colors: 'blue' }, | |
{ code: '2xx', source: 'internal', colors: 'blue' }, | |
{ code: '3xx', source: 'internal', colors: 'blue' }, | |
{ code: '2xx', source: 'cdn', colors: 'red' }, | |
{ code: '3xx', source: 'cdn', colors: 'red' }, | |
{ code: '2xx', source: 'internal', colors: 'red' }, | |
{ code: '3xx', source: 'internal', colors: 'red' } | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment