扁平化数组

1.使用flat()方法

参数为想要扁平的层数,默认为1,使用Ifinity则不管嵌套多少层都转化成一维数组

2.使用reduce()递归

function bianpinghua(arr) {
    return arr.reduce((result, item) => {
        return result.concat(Array.isArray(item)?bianpinghua(item):item);
        },[]);
}

猜你喜欢

转载自www.cnblogs.com/LangZ-/p/13390496.html