js多維數組轉一維數組+數組去重+排序

function f (arr) {
     if (Object.prototype.toString.call(arr) !='[object Array]') {//判斷是否爲數組
        return;
      }
    var newarr = [];
    function fn (arr) {
       for (var i = 0; i < arr.length; i++) {
          if (arr[i].length) {
            fn(arr[i]);
          }else{
            newarr.push(arr[i]);
          }
        }
    }
    fn(arr);
    return newarr;
  }

  Array.prototype.u = function () {
     var newarr = [];
     var obj = {};
     for (var i = 0; i < this.length; i++) {
       if (!obj[this[i]]) {
          newarr.push(this[i]);
          obj[this[i]] = 1;
       }
     }
     return newarr;
  }
  function compare (c1,c2) {
     return c1 -c2;
  }
  var arr = [1,[[4,4,6],6,[[[7,8,9]]],3]];
  var a = [];
  a = f(arr);
  b = a.u();
  c = b.sort(compare);
  console.log(c);
相關文章
相關標籤/搜索