2018-6-21_《ES6数组扁平化函数》

const flattenArray = arr => {
    const flattened = [].concat(...arr);
        return flattened.some(v => Array.isArray(v))?flattenArray(flattened):flattened; 
}

const arr = [1,[2,3,[3.1,3.2]],4];
console.log(flattenArray(arr));  //[1, 2, 3, 3.1, 3.2, 4]

  

posted @ 2018-06-21 21:00  Coca-code  阅读(1106)  评论(0编辑  收藏  举报