3 ms·
arr.flat(Infinity) seems like a strange decision for flattening the entire array - wouldn't the most common number of levels to flatten an array be all levels,
by dawhizkid 7y ago
arr.flat(Infinity) seems like a strange decision for flattening the entire array - wouldn't the most common number of levels to flatten an array be all levels, in which case I'd expect arr.flat() to flatten the whole array but in this case it's just 1 level.
- calebegg 7y agoIf your array is contained in itself, flat(Infinity) gives a stack overflow. My guess is they wanted the default behavior to be safer/saner.
- kevlened 7y agoInfinity was originally the default (which feels intuitive), but here's the argument for changing it [0]: "I think Array#flatten should be shallow by default because it makes sense to do less work by default, it aligns with existing APIs like the DOM Node#cloneNode which is shallow by default, and it would align with the existing ES3 pattern of using Array#concat for a shallow flatten. Shallow by default would also align with flatMap too." [0] https://github.com/tc39/proposal-flatMap/issues/9 https://github.com/tc39/proposal-flatMap/issues/9
- dawhizkid 7y agoI’m trying to think of a single use case where I’d want to flatten exactly one level of an array and can’t.