by saranshk on 1/5/22, 3:18 PM with 89 comments
by heydenberk on 1/5/22, 4:32 PM
[1, 2, 3, 4, 5].filter(n => n % 2 === 1).map(n => n * 2)
You can do: [1, 2, 3, 4, 5].flatMap(n => n % 2 === 1 ? [n * 2] : [])
Again, this is a contrived example, but I think it's interesting since the generality is not obvious (to me)by yawnxyz on 1/5/22, 3:58 PM
I'm often delighted to find new convenience functions I can just use without adding another dependable.
by Waterluvian on 1/5/22, 3:40 PM
Is there some specific reason that `flatMap` made the cut?
by Rygian on 1/5/22, 4:05 PM
Example in the article is meaningless to me:
array.map(x => [x \* 2]);
// [[2], [4], [6], [8]]
array.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
because the right way would be array.map(x => x *2); anyway.What am I missing? What is a realistic scenario where an array needs to be flattened?
by _greim_ on 1/5/22, 5:38 PM
by vmchale on 1/5/22, 3:51 PM
by krylon on 1/5/22, 5:10 PM
by scotty79 on 1/5/22, 6:38 PM