We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
flat函数就是扁平化嵌套数组,例如:
var arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4]
function flat(arr){ return arr.flat(Infinity) } flat([1, [2, [3, [4, [5]]]]]) // [1, 2, 3, 4, 5]
[1, [2, [3, [4, [5]]]]].toString().split(',').map(v => +v) // [1, 2, 3, 4, 5]
function flat(arr){ return arr.reduce((pre, next) => pre.concat(Array.isArray(next) ? flat(next) : [next]), []) } flat([1, [2, [3, [4, [5]]]]]) // [1, 2, 3, 4, 5]
function flat(arr){ if (!Array.isArray(arr)) return arr var res = [] var stack = [...arr] while(stack.length) { var next = stack.shift() if (Array.isArray(next)) { stack.push(...next) } else { res.push(next) } } return res } flat([1, [2, [3, [4, [5]]]]])
The text was updated successfully, but these errors were encountered:
sihai00
No branches or pull requests
实现flat函数
flat函数就是扁平化嵌套数组,例如:
1. Array.prototype.flat
2. 字符串化
3.concat和reduce递归
4.循环判断
The text was updated successfully, but these errors were encountered: