jsは多次元配列を実現して一次元配列を回転します.

1625 ワード

var aArray=[1,[2,3],[2,[2,3,4,5]],[6,7],8];
var cahr=aArray.toString();//      
cahr=cahr.split(",")//  ,       
cahr=Array.from(new Set(cahr));//  
cahr.sort(function(x,y){//  
    return y-x;
})
console.log(cahr);//["8", "7", "6", "5", "4", "3", "2", "1"]
var arr=[1,[2,3],[2,[2,3,4,5]],[6,7],8];
console.log(arr.flat(Infinity));//ES6   ,  es6   ,           

var arr=[1,[2,3],[2,[2,3,4,5]],[6,7],8];
var new_Array=[];
function recursion(arr) {//  
if (arr instanceof Array) {
for (var j = 0; j < arr.length; j++) {
if (arr[j] instanceof Array) {//     ,       
recursion(arr[j])
}else{
new_Array.push(arr[j]);//    ,      
}
}
}
return new_Array;
}
var newArr=recursion(aArray);
newArr=Array.from(new Set(newArr));//  
newArr.sort(function(x,y){//      
return y-x;
})
console.log(newArr);//  [8, 7, 6, 5, 4, 3, 2, 1]
 
var aArray=[1,[2,3],[2,[2,3,4,5]],[6,7],8];
var new_Array=[];
function isArray(aArray) {//       
return aArray && ( typeof aArray === 'object') && (aArray.constructor == Array);
}
function recursion(aArray) {//  
if (isArray(aArray)) {
for (var j = 0; j < aArray.length; j++) {
if (!isArray(aArray[j])) {//      ,     
new_Array.push(aArray[j]);
continue;
}
recursion(aArray[j]);//     ,       
}
}
}
var newArr = [];
function getArrNElementNum(aArray) {
recursion(aArray);
for(var i=0, len=new_Array.length; i