MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/b2o6f2/whats_your_favorite_most_useful_javascript/eiuvfad
r/javascript • u/Linux_is_awesome • Mar 18 '19
422 comments sorted by
View all comments
2
Not usually one lined, but short and useful nonetheless
const splitBy = (fn, arr) => arr.reduce( ([ l, r ], c) => fn(c) ? [ l.concat(c), r ] : [ l, r.concat(c) ], [[],[]] ) // eg const [ odd, even ] = splitBy( (x) => x % 2, [ 1, 2, 3, 4, 5 ] )
1 u/timewast3r Mar 19 '19 FTFY 😁const splitBy=(c,t)=>t.reduce(([t,e],n)=>c(n)?[t.concat(n),e]:[t,e.concat(n)],[[],[]]),[odd,even]=splitBy(c=>c%2,[1,2,3,4,5]);
1
FTFY 😁
const splitBy=(c,t)=>t.reduce(([t,e],n)=>c(n)?[t.concat(n),e]:[t,e.concat(n)],[[],[]]),[odd,even]=splitBy(c=>c%2,[1,2,3,4,5]);
2
u/e13e7 Mar 19 '19
Not usually one lined, but short and useful nonetheless