DEV Community

loading...

Discussion on: Demystifying Array.prototype.flat

Collapse
turnerj profile image
James Turner • Edited

Interesting - without looking at the docs or anything, I would have (incorrectly) assumed:

arr.flat()

was to to flatten all the way down like Laurie's last example

arr.flat(Infinity)
Collapse
georgecoldham profile image
George

This is why I thought it should be mentioned. It kept catching me out when I was trying it out.

Thread Thread
laurieontech profile image
Collapse
laurieontech profile image
Laurie Author

Apparently that’s a lot of people’s assumption which actually surprises me!

Thread Thread
turnerj profile image
James Turner

I wonder if I am thinking of it similar to how substr works with the second argument.

"abc".substr(1)
"abc".substr(1, Infinity)

These both return "bc" and you don't need to worry about actually specifying the end of the string. I see this in the same way as flat(), it just seems like the more appropriate way of working.

I wonder now what the most common call to flat actually will be, whether it will be specifying a specific level or many just using Infinity.

Forem Open with the Forem app