php array 递归
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,.
在Array.prototype.flat
的早期阶段就进行了很多讨论,仅从名称开始。 许多开发人员更喜欢使用flatten
这个名称,但其规范与MooTools的实现有所不同。 MooTools会递归地展平数组,但是新的官flat
实现默认为展平一个级别。
The current implementation of Array.prototype.flat
is:
Array.prototype.flat
的当前实现是:
[1, 2, [3], [[4]]].flat(/* depth */);
// [1,2,3,[4]]
.flat
only flattens arrays to one level by default, but what if you want a truly flattened array? You can use Infinity
and flat
's depth
argument to make that happen:
默认情况下, .flat
仅将数组.flat
平到一个级别,但是如果您想要真正展平的数组怎么办? 您可以使用Infinity
和flat
的depth
参数来实现:
[1, 2, [3], [[4]], [[[[[[6]]]]]]].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 stringify
!
我发现方法名称有点令人误解,但我理解为什么它们进入了单个级别。 方法名称smush
被抛出,这是自stringify
以来最糟糕的方法名称!
php array 递归