I'm a full stack Typescript developer specialising in Angular and Node. I love reactive programming with RxJS, utilising the less-used web APIs and I'm passionate about polymorphic full stack TS.
could you ever shoot yourself in the foot with this call, or does it protect me from my own shenanigans?
Yep you can shoot yourself in the foot - if for some reason your array contains circular references .flat(Infinity) will cause a stack overflow.
demo:
a=[1,2,3]b=[4,5,6]a.push(b)b.push(a)a.flat(Infinity)// VM185:9 Uncaught RangeError: Maximum call stack size exceeded// at Array.flat (<anonymous>)// at <anonymous>:9:3
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
Does anyone know why the
arr.flat()
doesn't automatically useInfinity
as the default value? Is it a performance thing or something?Also, could you ever shoot yourself in the foot with this call, or does it protect me from my own shenanigans?
Yep you can shoot yourself in the foot - if for some reason your array contains circular references
.flat(Infinity)
will cause a stack overflow.demo:
Depends what the shenanigans are! š
And to be honest, Iām surprised so many people expected it to be infinity by default?
It make sense,
flat(N)
means I have to know exactly how many levels of nesting there are. We're lazy,Infinity
should have been the default :P