Problem?
How to put arr = [[1,3,4,5],[2,3,5]] into one dimension? For example: arr = [1,3,4,5,2,3,5];
What if there are more dimensions? For example: arr=[2,3,3,4,[2,3,4,[13,3,[3,4,6],4]];;
Scenario One: Recursive test
arr=[[1,3,4,5],[2,3,5]]; var arr1=[]; function Addnum (e) {E.foreach (function (e) {if (typeof (e)! = "Number") {E.foreach (function (e) {addnum (e);})} Else{arr1.push (e);}})} Arr.foreach (function (e) {addnum (e); }); Console.log (ARR1);
Simple dimensions can also, but the array dimension becomes a lot of cases, recursion is not very useful ah?
Scenario Two: special handling of arrays
Arr=[2,3,3,4,[2,3,4,[13,3,[3,4,6],4]]];arr= "" +arr; Here the array is processed console.log (arr); 2,3,3,4,2,3,4,13,3,3,4,6,4arr=arr.split (","); Console.log (arr); ["2", "3", "3", "4", "2", "3", "4", "", "3", "3", "4", "6", "4"]var Arr1=[];for (var i=0;i<arr.length;i++) { arr1 [I]=eval (Arr[i]);} Console.log (arr1); [2, 3, 3, 4, 2, 3, 4, 13, 3, 3, 4, 6, 4];
Even an array that handles higher dimensions is also possible.
Method Two skillfully use the implicit conversion of the array and the string, and then use the string method, the string is divided into a string array, and then into a numerical array.
Scatter multidimensional arrays--multi-dimensional array descending order