zoukankan      html  css  js  c++  java
  • 数组降维-递归的归纳整理——数组降维的方法有三种半

    1、朴素的转换

    二维数组降维

    这里有个容易出现的问题就是,必须要对元素进行是否是数组的判断,因为数组和字符串都有length属性,是数组的话,就是降维操作,是字符串的话,就会把字符串拆解了;还有就是数字没有length,所以数字元素的length的值是报错的,当前for循环无法执行,只有是数组或字符串才会有输出,所以必须要判断元素的属性

     1 var arrs = ['3', 'we', 'w3', ['e', 'g'], 1, 2, 3, [4, 5]];
     2 
     3 function jiangwei(arrs) {
     4   var newarrs = [];
     5   for (var i = 0; i < arrs.length; i++) {
     6     if (arrs[i] instanceof Array) {
     7       for (var j = 0; j < arrs[i].length; j++) {
     8         newarrs.push(arrs[i][j])
     9       }
    10     } else {
    11       newarrs.push(arrs[i])
    12     }
    13   }
    14   return newarrs
    15 }
    16 jiangwei(arrs);//["3", "we", "w3", "e", "g", 1, 2, 3, 4, 5]

    多维数组降维

     1 var arrs = [1, [23, 32], 42, [0, 33, [55, 39, [10, 331, [15, 34]]]]];
     2 
     3 function jiangwei1(arrs) {
     4   var newarrs = [];
     5   function digui(arrs) {
     6     for (var i = 0; i < arrs.length; i++) {
     7       if (arrs[i] instanceof Array) {
     8         digui(arrs[i]);
     9       } else {
    10         newarrs.push(arrs[i]);
    11       }
    12     }
    13   }
    14   digui(arrs);
    15   return newarrs
    16 }
    17 jiangwei1(arrs);//[1, 23, 32, 42, 0, 33, 55, 39, 10, 331, 15, 34]

    2、使用数组的concat方法

    二维数据降维

     1 var arrs = ['3', 'we', 'w3', ['e', 'g'], 1, 2, 3, [4, 5]];
     2 
     3 function jiangwei2(arrs){
     4   var newarrs=[];
     5   for(var i=0;i<arrs.length;i++){
     6       newarrs=newarrs.concat(arrs[i])
     7   }
     8   return newarrs
     9 }
    10 jiangwei2(arrs);//["3", "we", "w3", "e", "g", 1, 2, 3, 4, 5]

    多维数组降维

     1 var arrs = [1, ['3', 'we',], 42, [0, 33, [55,  [10, 331, [15, 34]]]]];
     2 
     3 function jiangwei3(arrs){
     4   var newarrs=[];
     5   function digui(arrs){
     6     for (var i=0;i<arrs.length;i++){
     7       if (arrs[i] instanceof Array){
     8           digui(arrs[i]);
     9       }else{
    10         newarrs=newarrs.concat(arrs[i]);
    11       }
    12     }
    13   }
    14   digui(arrs);
    15   return newarrs
    16 }
    17 jiangwei3(arrs);//[1, "3", "we", 42, 0, 33, 55, 10, 331, 15, 34]

    3、利用applyconcat转换

    二维数组降维

    1 var arrs = ['3', 'we', 'w3', ['e', 'g'], 1, 2, 3, [4, 5]];
    2 
    3 function jiangwei4(arrs){
    4   return Array.prototype.concat.apply([],arrs);
    5 }
    6 jiangwei4(arrs);//["3", "we", "w3", "e", "g", 1, 2, 3, 4, 5]

    多维数组降维

     1 var arrs = [1, ['3', 'we',], 42, [0, 33, [55, [10, 331, [15, 34]]]]];
     2 
     3 function jiangwei5(arrs) {
     4   for (var i = 0; i < arrs.length; i++) {
     5     if (arrs[i] instanceof Array) {
     6       arrs[i] = jiangwei5(arrs[i]);
     7     }
     8   }
     9   return Array.prototype.concat.apply([], arrs);
    10 }
    11 jiangwei5(arrs);//[1, "3", "we", 42, 0, 33, 55, 10, 331, 15, 34]

    4、toSthing方法

    在研究降维的过程中,使用了数组的toSting方法,另一个角度看,也算是数组的降维了,当有局限性,全部是字符串的数组,是没有问题的,带有数字的数组,数字也会变成字符串,

    而且其他类型也都会有所变化

    String-String

    Number-String

    Boolean-String

    Object-String-[object Object]

    Function-String-函数表达式

    Null-空白

    Undefined-空白

    1 var arrs = [1, ['3', 'we',], 42, [0, 33, [55, [10, 331, [15, 34]]]]];
    2 arrs.toString();//"1,3,we,42,0,33,55,10,331,15,34"
    3 arrs.toString().split(",");//["1", "3", "we", "42", "0", "33", "55", "10", "331", "15", "34"]
    1 var arrs=[1,[2,'3'],[{'a':33},function(){alert(333)}],[4,[true,[null,[undefined]]]]];
    2 arrs.toString();//"1,2,3,[object Object],function (){alert(333)},4,true,,"
    3 arrs.toString().split(",");//["1", "2", "3", "[object Object]", "function (){alert(333)}", "4", "true", "", ""]
  • 相关阅读:
    Mybatis批量插入,是否能够返回id列表
    SVN和Git代码管理小结
    SVN和Git代码管理小结
    Spring异步执行(@Async)2点注意事项
    Spring异步执行(@Async)2点注意事项
    2015年工作中遇到的问题101-110
    Codeforces 263B. Appleman and Card Game
    Codeforces 263A. Appleman and Easy Task
    HDU 482 String
    字符串hash-BKDRHash
  • 原文地址:https://www.cnblogs.com/webzhanghua/p/5561515.html
Copyright © 2011-2022 走看看