数组扁平化

1、一维的嵌套数组

const flattenArr = (list = []) => list.reduce((total, cur) => {
    total.concat(cur);
    return total;
}, []);

2、通用的方法,数组嵌套数组

const flattenArr = (list = []) => list.reduce((total, cur) => {
    if (Array.isArray(cur)) {
        return total.concat(flattenArr(cur));
    }
    return total.concat(cur);
});

也可以用

function flatten(arr) {
    return arr.join(',').split(',').map(function (item) {
        return parseInt(item)
    })
}
原文地址:https://www.cnblogs.com/jwenming/p/14581016.html