Bifurcate

    0

    0

    monizb

    This snippet splits values into two groups, based on a predicate function. If the predicate function returns a truthy value, the element will be placed in the first group. Otherwise, it will be placed in the second group.

    const bifurcateBy = (arr, fn) =>
      arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);
    bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b'); 
    // [ ['beep', 'boop', 'bar'], ['foo'] ]
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.