Array.prototype.reduce()
combined with Array.prototype.map()
to iterate over elements and combine into an array containing all combinations.const powerset = arr =>
arr.reduce((a, v) => a.concat(a.map(r => r.concat(v))), [[]]);
powerset([1, 2]); // [[], [1], [2], [1, 2]]
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️