_.flatten
Flattens array a single level deep.
- // Underscore/Lodash
- _.flatten([1, [2, [3, [4]], 5]]);
- // => [1, 2, [3, [4]], 5]
- // Native
- const flatten = [1, [2, [3, [4]], 5]].reduce( (a, b) => a.concat(b), [])
- // => [1, 2, [3, [4]], 5]
- // Native(ES2019)
- const flatten = [1, [2, [3, [4]], 5]].flat()
- // => [1, 2, [3, [4]], 5]
Browser Support for Array.prototype.reduce()
46.0 ✔ | ✔ | 3.0 ✔ | 9.0 ✔ | 10.5 ✔ | 4 ✔ |
Browser Support for Array.prototype.flat()
69 ✔ | ✖ | 62 ✔ | ✖ | 56 ✔ | 12 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .