【JavaScript 教程】第六章 数组16—flat() :递归地将数组展平到指定的深度
web前端开发
共 2081字,需浏览 5分钟
·
2022-02-22 09:00
let newArray = arrayObject.flat([depth])
const numbers = [1, 2, [3, 4, 5]];
const flatNumbers = numbers.flat();
console.log(flatNumbers);
[1, 2, 3, 4, 5]
console.log(numbers);
[ 1, 2, [ 3, 4, 5 ] ]
const numbers = [1, 2, [3, 4, 5, [6, 7]]];
const flatNumbers = numbers.flat(2);
console.log(flatNumbers);
[1, 2, 3, 4, 5, 6, 7]
const numbers = [1, 2, [3, 4, 5, [6, 7, [8, 9]]]];
const flatNumbers = numbers.flat(Infinity);
console.log(flatNumbers);
const numbers = [1, 2, , 4, , 5];
const sequence = numbers.flat();
console.log(sequence);
[ 1, 2, 4, 5 ]
学习更多技能
请点击下方公众号
评论