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);
时间: 2024-11-04 02:09:57