I 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…
myFriends: { name: string, friends: friend[] }[]
If I wanted to get a list of friends of friends, I could do something like
myFriends.map(prop('friends')).flatten()
Or
myFriends.flatMap(prop('friends'))
My data is a list of lists but I really want a list of friends so I flatten.
Maybe the Friend object is poorly created/designed but often times you'll just have to deal with whatever the API gives you.