Comment

Comment by Rygian

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 missing? What is a realistic scenario where an array needs to be flattened?

parent Rygian 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton