美文网首页
数组扁平化处理

数组扁平化处理

作者: _theFeng | 来源:发表于2019-03-19 17:22 被阅读0次
    // 数组扁平化排序处理
    var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14]]]], 10];
    //1
    Array.prototype.flat = function () {
     return [].concat(...this.map(item => (Array.isArray(item) ? item.flat() : item)));
    }
    
    Array.prototype.unique = function () {
     return [...new Set(this)]
    }
    
    const sort = (a, b) => a - b;
    
    console.log(arr.flat().unique().sort(sort));
    //2
    console.log([...new Set(String(arr).split(','))].map(Number).sort((a,b)=>a-b))
    //3
    console.log([...new Set(arr.join(',').split(','))].map(Number).sort((a, b) => a - b))
    

    相关文章

      网友评论

          本文标题:数组扁平化处理

          本文链接:https://www.haomeiwen.com/subject/dbenmqtx.html