Skip to content

Instantly share code, notes, and snippets.

@jaidetree
Last active May 3, 2017 19:32
Show Gist options
  • Select an option

  • Save jaidetree/d748b4707ce0ba9b2c16285f7a4ffe8e to your computer and use it in GitHub Desktop.

Select an option

Save jaidetree/d748b4707ce0ba9b2c16285f7a4ffe8e to your computer and use it in GitHub Desktop.
let data = [
{
label: 'Food',
value: 1,
},
{
label: 'Hors D\'Ouvers',
value: 2,
},
{
label: 'Meal',
value: 3,
},
];
function combiner (options) {
let columnIndex = 0;
let items = [];
while (columnIndex < options.length) {
let itemA = options[columnIndex];
let rowIndex = 0;
items.push({
label: itemA.label,
value: [itemA.value],
});
while (rowIndex < options.length) {
let itemB = options[rowIndex];
let isContained = items.some(({ value }) => {
return value.includes(itemA.value) && value.includes(itemB.value);
});
if (!isContained && itemA.value !== itemB.value) {
items.push({
label: `${itemA.label} & ${itemB.label}`,
value: [ itemA.value, itemB.value ],
});
}
rowIndex += 1;
}
columnIndex += 1;
}
return items;
}
console.log(combiner(data).map(item => {
item.value = item.value.join(',');
return item;
}));
// => [ { label: 'Food', value: '1' },
// { label: 'Food & Hors D\'Ouvers', value: '1,2' },
// { label: 'Food & Meal', value: '1,3' },
// { label: 'Hors D\'Ouvers', value: '2' },
// { label: 'Hors D\'Ouvers & Meal', value: '2,3' },
// { label: 'Meal', value: '3' } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment