This function generates all possible subsets of an array.
JavaScript:
let subsets = arr => arr.reduce((subs, value) => subs.concat(subs.map(sub => [value, ...sub])), [[]]);
// Example
console.log(subsets([1, 2])); // [[], [2], [1], [1, 2]]
console.log(subsets(['a', 'b'])); // [[], ['b'], ['a'], ['a', 'b']]
TypeScript:
let subsets = (arr: T[]): T[][] => arr.reduce((subs, value) => subs.concat(subs.map(sub => [value, ...sub])), [[]]);
// Example
console.log(subsets([1, 2])); // [[], [2], [1], [1, 2]]
console.log(subsets(['a', 'b'])); // [[], ['b'], ['a'], ['a', 'b']]