js数组深度去重

 // 数组深度去重
    let arr = [1, 2, 3, 4, 1, 4, 5, 5, {a: false, b: {c: 2}}, {a: 0, b: {c: 2}}, {a: 0, b: {c: 2}}, {a: 0, b: {c: 2}}, [1, 2], [1, 2]];
    console.log('org', arr)
    Array.prototype.noRepeat = function(){
      let obj = {};
      return this.filter( el => obj.hasOwnProperty( typeof el + JSON.stringify(el) ) ? false : (obj[typeof el +  JSON.stringify(el) ] = true) )
    }
    let nArr = arr.noRepeat()
    console.log( nArr )

    //简化
    Array.prototype.noRepeat = function(){
      let obj = {};
      return this.filter( el => obj[ JSON.stringify(el) ] ? false : (obj[ JSON.stringify(el) ] = true) )
    }

 

posted @ 2019-03-11 17:38  JeneryYang  阅读(373)  评论(0编辑  收藏  举报