Scatter multidimensional arrays--multi-dimensional array descending order

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.