num: 可选参数,展开嵌套层数
let ary = [1, 2, 3, [4, 5]] console.log(ary.flat()) // [ 1, 2, 3, 4, 5 ] let ary1 = [1, 2, 3, [4, 5, [6, 7]]] console.log(ary1.flat()) // [ 1, 2, 3, 4, 5, [ 6, 7 ] ] let ary2 = [1, 2, 3, [4, 5, [6, 7]]] console.log(ary2.flat(2)) // [ 1, 2, 3, 4, 5, 6, 7 ] // 参数大于层数时,展开所有嵌套 let ary3 = [1, 2, 3, [4, 5, [6, 7]]] console.log(ary3.flat(3)) // [ 1, 2, 3, 4, 5, 6, 7 ] let ary4 = [1, 2, 3, [4, 5], {name: 'tom', age: 18}] console.log(ary4.flat()) // [ 1, 2, 3, 4, 5, { name: 'tom', age: 18 } ] // 有对象嵌套时,仍然展开 let ary5 = [1, 2, 3, [4, 5, {name: 'xiao', age: 20}], {name: 'tom', age: 18}] console.log(ary5.flat()) // [ 1, 2, 3, 4, 5, { name: 'xiao', age: 20 }, { name: 'tom', age: 18 } ] let ary = [1, 2, 3, [4, 5, [6, 7, { name: 'xiao', age: 20 }]], { name: 'tom', age: 18 }] console.log(ary.flat(4)) // [ 1, 2, 3, 4, 5, 6, 7, { name: 'xiao', age: 20 }, { name: 'tom', age: 18 } ] // 对象嵌套不能展开 let ary = [1, 2, 3, [4, 5, [6, 7]], { name: 'tom', age: 18, 'key': { name: 'xiao', age: 20 } }] console.log(ary.flat(4)) // [ 1, 2, 3, 4, 5, 6, 7, { name: 'tom', age: 18, key: { name: 'xiao', age: 20 } ]