Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
1–10 of 97 posts
Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#2Is there some specific reason that `flatMap` made the cut?
Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#3I've always found `flatMap` to be interesting because it feels like a convenience function, combining .map(fn).flat() into one function. It's interesting because JS doesn't really have a lot of convenience functions that are this shallow (ie. that provide just minimal cleanup compared to the functions they're wrapping). Is there some specific reason that `flatMap` made the cut?
Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#4Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#5I've always found `flatMap` to be interesting because it feels like a convenience function, combining .map(fn).flat() into one function. It's interesting because JS doesn't really have a lot of convenience functions that are this shallow (ie. that provide just minimal cleanup compared to the functions they're wrapping). Is there some specific reason that `flatMap` made the cut?
Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#6I've always found `flatMap` to be interesting because it feels like a convenience function, combining .map(fn).flat() into one function. It's interesting because JS doesn't really have a lot of convenience functions that are this shallow (ie. that provide just minimal cleanup compared to the functions they're wrapping). Is there some specific reason that `flatMap` made the cut?
I think conceptually, `flatMap` is a `map` where each iteration can return multiple values (or none). So it feels quite more powerful than just `map` and is a quite common convenience function.
Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#7I'm often delighted to find new convenience functions I can just use without adding another dependable.
Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#8Example 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?
Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#9I've made it a habit to check Mozilla's JS docs once in a while for functions like Flat() and FlatMap(). Sometimes if I find myself reaching for underscore/lodash, I'll check Mozilla docs first to see if there's some new function that can let me omit using lodash. I'm often delighted to find new convenience functions I can just use without adding another dependable.
E.g.
sumBy(“orders”, “total.amount”)Re: Flatten Arrays in Vanilla JavaScript with Flat() and FlatMap()
#10I have very little experience with arrays in JS, but I always have a nagging feeling that, if my algorithm needs me to flatten an array, then there is something wrong with the data structures I am using. 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 missi…
parents.flatMap(parent => parent.children)