There was much talk about
Array.prototype.flat during its early stages, starting with the name alone. Many developers preferred the name
flatten but the spec differed from MooTools’ implementation. MooTools would recursively flatten an array but the new, official
flat implementation defaults one level of flattening,.
The current implementation of
[1, 2, , []].flat(/* depth */); // [1,2,3,]
.flat only flattens arrays to one level by default, but what if you want a truly flattened array? You can use
depth argument to make that happen:
[1, 2, , [], [[[[[]]]]]].flat(Infinity); // [1,2,3,4,6]
I find the method name a bit misleading but I understand why they went to a single level. The method name
smush was thrown around, which would’ve been the worst method name since
The post Recursive Array.flat appeared first on David Walsh Blog.