Split Array

    0

    0

    monizb

    This snippet splits values into two groups and then puts a truthy element of filterin the first group, and in the second group otherwise.

    const bifurcate = (arr, filter) =>
      arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
    bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); 
    // [ ['beep', 'boop', 'bar'], ['foo'] ]
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.