Javascript-遍历嵌套数组

时间:2018-07-22 06:27:17

标签: javascript arrays

我正在将较长的数组转换为较小数组的嵌套数组。对于嵌套数组中的每个数组,我想对数组中的元素进行一些计算,然后将结果推入数组中。

    //Split bigger array into smaller arrays....this function works
    function getArray() {
      items = ['image1','place','name',10,20,30,40,50,60,'image1','place','name',11,21,31,41,51,61];
      img = "image1"
      var size = 9;
      var newarr = [];
      for (var i = 0; i < items.length; i+=size) {
          newarr.push(items.slice(i, i+size));  
      }
      return newarr;
    }
    

    function fourthCoord(arr) {
      for (var i = 0; i < arr.length; i++) {
        for (var j=0; j < arr[i].length; j++) {
          var newarr = arr;
          firstx = newarr[j][3];
          secondx = newarr[j][5];
          var thirdd = secondx-firstx;
          thirdx = newarr[j][7];
          var fourthx = thirdx-thirdd
          newarr.push(fourthx);
          return newarr;  
        }
      }
    }
      
    
    function init(){
      var ctx = document.getElementById('canvas').getContext('2d');
      ctx.canvas.addEventListener('click', function(event) {
       var arr = new Array();
        arr = fourthCoord(getArray(arr));
        console.log(arr);
    })
    }
    
    window.addEventListener('load', init, false);
 
 <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>array</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
      <canvas id="canvas" width="800" height="600" style="border:solid 1px;margin:0;padding:0;"></canvas> 
    </body>
    </html>
    
    

fourthCoord函数将结果值作为元素推送到数组之外,而不是每个数组中。

[Array(9), Array(9), 30]
["image1", "place", "name", 10, 20, 30, 40, 50, 60]
["image1", "place", "name", 11, 21, 31, 41, 51, 61]
30

所需的输出是:

[Array(9), Array(9)]
    ["image1", "place", "name", 10, 20, 30, 40, 50, 60, 30]
    ["image1", "place", "name", 11, 21, 31, 41, 51, 61, 30]

1 个答案:

答案 0 :(得分:1)

您不需要重复两次循环。这是您可以在1个循环中轻松完成此操作的方式:

//Split bigger array into smaller arrays....this function works
    function getArray() {
      items = ['image1','place','name',10,20,30,40,50,60,'image1','place','name',11,21,31,41,51,61];
      img = "image1"
      var size = 9;
      var newarr = [];
      for (var i = 0; i < items.length; i+=size) {
          newarr.push(items.slice(i, i+size));  
      }
      return newarr;
    }
    

    function fourthCoord(arr) {
    var finalarr = [];
    var newarr =[];
      for (var i = 0; i < arr.length; i++) {
  
     
           newarr = arr[i];
          firstx = newarr[3];
          secondx = newarr[5];
          var thirdd = secondx-firstx;
          thirdx = newarr[7];
          var fourthx = thirdx-thirdd
          newarr.push(fourthx);
        finalarr.push(newarr);  
     
      }
      
       return finalarr;  
    }
      
    
    function init(){
      var ctx = document.getElementById('canvas').getContext('2d');
      ctx.canvas.addEventListener('click', function(event) {
       var arr = new Array();
        arr = fourthCoord(getArray(arr));
        console.log(arr);
    })
    }
    
    window.addEventListener('load', init, false);
<!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>array</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
      <canvas id="canvas" width="800" height="600" style="border:solid 1px;margin:0;padding:0;"></canvas> 
    </body>
    </html>