我在尝试拼接数组时遇到了麻烦

时间:2017-05-22 22:24:55

标签: javascript html css

地狱:

我一直把头发拉过来。我试图拼接一个多维数组我要删除具有0的行的索引,然后将零推到最后。我能够将零推到最后但索引列2位置2的0不会被删除。我已经尝试了所有的事情,并认为我可以寻求帮助,指出我正确的方向。我真的需要得到这个'这是我的代码,我感谢任何帮助。



(function() {
  'use strict';

  var data = [
    ['e', 0, 1, 2, 3, 4],
    ['a', 54312, 235, 5, 15, 4],
    ['a', 6, 7, 8, 9, 232],
    ['a', 54, 11235, 345, 5, 6],
    ['b', 0, 1, 2, 3, 4],
    ['b', 54312, 235, 5, 15, 4],
    ['c', 62, 15, 754, 93, 323],
    ['d', 27, 11235, 425, 18, 78],
    ['d', 0, 1, 2, 3, 4],
    ['d', 54312, 235, 5, 15, 4],
    ['e', 6, 7, 8, 9, 232],
    ['e', 54, 11235, 345, 5, 6],
    ['e', 0, 1, 2, 3, 4],
    ['e', 54312, 235, 5, 15, 4],
    ['e', 62, 15, 754, 93, 323],
    ['e', 27, 11235, 425, 18, 78]
  ];

  //Create a HTML Table element.
  var table = document.createElement("TABLE");
  var somedata = document.createElement("TD");

  //Get the count of columns.
  var columnCount = data[0].length;

  var count = 0;
  //Add the data rows.  
  for (var i = 0; i < data.length; i++) {
    var row = table.insertRow(-1);
    for (var j = 0; j < columnCount; j++) {
      var cell = row.insertCell(-1);
      cell.innerHTML = data[i][j];
      cell.innerHtml = myZero;
      var myZero = data[i][j]
      if (myZero === 0) {
        console.log(i, j, cell);
        console.log("is equal");
        console.log(data[myZero].splice(2, 1));
        data[myZero].push(0);
      }
    }
  }

  var dvTable = document.getElementById("dvTable");
  dvTable.innerHTML = "";
  dvTable.appendChild(table);

})();
&#13;
table {
  border-collapse: collapse;
  border-left: 1px solid #999999;
  border-top: 1px solid #999999;  
}
td {
  border-right: 1px solid #999999;
  border-bottom: 1px solid #999999;
}
&#13;
<div id="dvTable"></div>
&#13;
&#13;
&#13;

提前感谢你 PHD

1 个答案:

答案 0 :(得分:1)

你有一些乱序,你的拼接引用了静态值,所以我总是拼接数组中的第三个项目(位置2)。你也总是将0推入数组数组中的第一个数组(data[myZero].push(0););

&#13;
&#13;
(function() {
  'use strict';


  var data = [
    ['e', 0, 1, 2, 3, 4],
    ['a', 54312, 235, 5, 15, 4],
    ['a', 6, 7, 8, 9, 232],
    ['a', 54, 11235, 345, 5, 6],
    ['b', 0, 1, 2, 3, 4],
    ['b', 54312, 235, 5, 15, 4],
    ['c', 62, 15, 754, 93, 323],
    ['d', 27, 11235, 425, 18, 78],
    ['d', 0, 1, 2, 3, 4],
    ['d', 54312, 235, 5, 15, 4],
    ['e', 6, 7, 8, 9, 232],
    ['e', 54, 11235, 345, 5, 6],
    ['e', 0, 1, 2, 3, 4],
    ['e', 54312, 235, 5, 15, 4],
    ['e', 62, 15, 754, 93, 323],
    ['e', 27, 11235, 425, 18, 78]
  ];


  //Create a HTML Table element.
  var table = document.createElement("TABLE");
  var somedata = document.createElement("TD");


  //Get the count of columns.
  var columnCount = data[0].length;

  var count = 0;
  //Add the data rows.  
  for (var i = 0; i < data.length; i++) {
    var row = table.insertRow(-1);
    for (var j = 0; j < columnCount; j++) {
      var myZero = data[i][j]
      if (myZero === 0) {
        console.log(i, j, cell);
        console.log("is equal");
        console.log(data[i].splice(j, 1));
        data[i].push(0);
      }
      var cell = row.insertCell(-1);
      cell.innerHTML = data[i][j];
      cell.innerHtml = myZero;
    }
  }

  var dvTable = document.getElementById("dvTable");
  dvTable.innerHTML = "";
  dvTable.appendChild(table);


})();
&#13;
<div id="dvTable"></div>
&#13;
&#13;
&#13;