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
提示:Array.prototype.flatMap 已经是 EcmaScript 的标准。
The text was updated successfully, but these errors were encountered:
Array.prototype.myFlatMap = function (callback) { let array = this const result = [] const getArrayDepth = (arr) => { if (!Array.isArray(arr)) throw new Error('参数必须是数组') const map = { '[': 1, ']': -1 } let max = 0 let sum = 0 const str = JSON.stringify(arr).split('') str.forEach(item => { if (map[item]) { sum += map[item] if (sum > max) { max = sum } } }) return max } for (let index = 0; index < array.length; index++) { const element = array[index] let target = callback(element) if (Array.isArray(target)) { if (getArrayDepth(target) > 1) { target = target.flat() result.push(target) } else { result.push(...target) } } else { result.push(target) } } return result }
Sorry, something went wrong.
测试:
[1,2,3,4].myFlatMap(x => [[x * 2]]) // [[2],[4],[6],[8]] [1,2,3,4].myFlatMap(x => [[x * 2, x * 3]]) // [[2,3],[4,6],[6,9],[8,12]] [1,2,3,4].myFlatMap(x => [[[x * 2, x * 3]]]) // [[[2,3]],[[4,6]],[[6,9]],[[8,12]]]
No branches or pull requests
提示:Array.prototype.flatMap 已经是 EcmaScript 的标准。
The text was updated successfully, but these errors were encountered: