Skip to content

Instantly share code, notes, and snippets.

@mytharcher
Created September 17, 2019 07:40

Revisions

  1. mytharcher created this gist Sep 17, 2019.
    22 changes: 22 additions & 0 deletions generateCombinationsFromLists.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    const lists = [
    [1, 2],
    [3, 4],
    [5],
    [7]
    ];

    const combinations = lists
    .reduce((all, current) => current
    .map(item => all.map(g => g.concat([item])))
    .reduce((prev, curr) => prev.concat(curr), [])
    , [[]]);

    console.log(combinations);
    /*
    [
    [ 1, 3, 5, 7 ],
    [ 2, 3, 5, 7 ],
    [ 1, 4, 5, 7 ],
    [ 2, 4, 5, 7 ]
    ]
    */