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
// 递归实现 function flatten1(array = []) { if(!array || !array.length) return array var result = [] for(var i = 0, len = array.length; i < len; i++) { if(Array.isArray(array[i])) { result = result.concat(flatten1(array[i])) } else { result.push(array[i]) } } return result } // toString // 仅适用于都是string类型或都是number类型的去重 function flatten2(array = [], type = 'number') { if(!array || !array.length) return array return array.toString().split(',').map(v => type === 'number' ? +v : v) } // reduce function flatten3(array = []) { if(!array || !array.length) return array const result = array.reduce((acc, cur) => { return acc.concat(Array.isArray(cur) ? flatten3(cur): cur) }, []) return result } // es2019 - flat function flatten4(array = []) { if(!array || !array.length) return array return array.flat(Infinity) } var arr = [1, 2, 3, [4, 5, [6, 7, 8], [9]]] console.log(flatten1(arr),flatten2(arr), flatten3(arr), flatten4(arr))
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: