JavaScript将更多变量推送到第一个索引

时间:2015-04-14 14:15:11

标签: javascript arrays dictionary html-table

我对JavaScript数组有疑问。

我的问题是,如何使用for循环进行此操作?

你能帮助我吗?

JSFiddle:http://jsfiddle.net/aldimeola1122/hfpzw9nq/

/*
 *  Here I have created an array and saved the rows in this array. 
 */

var headers = $("th", $(".filterable")).map(function() {
  return this.innerHTML;
}).get();

var rows = $("tbody tr", $(".filterable")).map(function() {
  return [$("td", this).map(function() {
    return this.innerHTML;
  }).get()];
}).get();


var completeRow = [];
var completeRow = [
  rows[1][0] + ' ' + rows[1][1] + ' ' + rows[1][2],
  rows[2][0] + ' ' + rows[2][1] + ' ' + rows[2][2],
  rows[3][0] + ' ' + rows[3][1] + ' ' + rows[3][2],
  rows[4][0] + ' ' + rows[4][1] + ' ' + rows[4][2],
  rows[5][0] + ' ' + rows[5][1] + ' ' + rows[5][2],
  rows[6][0] + ' ' + rows[6][1] + ' ' + rows[6][2],
  rows[7][0] + ' ' + rows[7][1] + ' ' + rows[7][2],
  rows[8][0] + ' ' + rows[8][1] + ' ' + rows[8][2]
];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Here I have a table and each row should represent the first index of an array. -->

<table class="filterable">
  <tbody>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Tel</th>
    </tr>
    <tr>
      <td>AARON</td>
      <td>ABE</td>
      <td>04342123</td>
    </tr>
    <tr>
      <td>ABDUL</td>
      <td>ABEL</td>
      <td>$6.2bn</td>
    </tr>
    <tr>
      <td>ABRAHAM</td>
      <td>ABRAM</td>
      <td>343 234324 3242</td>
    </tr>
    <tr>
      <td>ADALBERTO</td>
      <td>ADAM</td>
      <td>43 234 234</td>
    </tr>
    <tr>
      <td>ADAN</td>
      <td>ADOLFO</td>
      <td>0343 234 32</td>
    </tr>
    <tr>
      <td>ADOLPH</td>
      <td>ADRIAN</td>
      <td>34 234 243</td>
    </tr>
    <tr>
      <td>AGUSTIN</td>
      <td>AHMAD</td>
      <td>324 234234</td>
    </tr>
    <tr>
      <td>AHMED</td>
      <td>ALAN</td>
      <td>4452 234 243</td>
    </tr>
  </tbody>

2 个答案:

答案 0 :(得分:1)

你的结果是jsfiddle

另外:

var completeRow = [];
for( var i = 1; i < rows.length; i++){
    if( completeRow[i-1] == undefined ){
      completeRow[i-1] = '';
    }
    for( var j = 0; j< rows[i].length; j++ ){
        completeRow[i-1] = completeRow[i-1] + ' '+ rows[i][j];
    }
}
alert(completeRow[0]);

<强>更新

链接到jsFiddle

答案 1 :(得分:0)

试试这个:

for (var i = 1; i <= rows.length; i++) {
  console.log(rows[i]);
}