1,使用map方法
var arr = [1,[2,[[3,4],5],6]]; function unid(arr){ var arr1 = (arr + '').split(',');//将数组转字符串后再以逗号分隔转为数组 var arr2 = arr1.map(function(x){ return Number(x); }); return arr2; } console.log(unid(arra));
2,使用递归方法
var arr = [1,[2,[[3,4],5],6]]; var newArr = []; function fun(arr){ for(var i=0;i<arr.length;i++){ if(Array.isArray(arr[i])){ fun(arr[i]); }else{ newArr.push(arr[i]); } } } fun(arr); console.log(newArr);//[1, 2, 3, 4, 5, 6]