如何使用javascript迭代HTML实体

时间:2017-05-11 11:44:59

标签: javascript arrays dom

我有以下HTML [下方]。我试图用class =" gmvcRow"迭代所有DOM,然后抓住所有" gmvcCell"中的文本。对于每个' gmvcRow'。我想将所有文字放在一个数组中[['名字','姓氏',' dob',' city']) 。我想把这个数组放到另一个数组中,该数组包含" gmvcRow"的数组。我的尝试在下面,但我仍然没有成功。我理解' gmvcCell'中的文字。本身就是另一个标签节点。

 <div class="gmvcRow">
      <div class="gmvcCell">firtname</div>
      <div class="gmvcCell">lastname</div>
      <div class="gmvcCell">dob</div>
      <div class="gmvcCell">city</div>
 </div>
 <div class="gmvcRow"></div>
 <div class="gmvcRow"></div>
 <div class="gmvcRow"></div>
 <div class="gmvcRow"></div>
 <div class="gmvcRow"></div>

我的代码:

 var gmvcRowArray = document.getElementsByClassName('gmvcRow');
 console.log('number of records: ' + gmvcRowArray.length);

 // Master array
var masterArray = [];

// Iterate through array
for(var i=0; i<gmvcRowArray.length; i++){
    // Iterate through all childNodes
    var rowArray = [];


    for(var c=0; c<gmvcRowArray[i].childNodes.length; c++){
        c = c + 1; // skip non text node
        console.log('c: '+c);

        if(gmvcRowArray[i].childNodes[c] != null){
            var value = gmvcRowArray[i].childNodes[c].innerHTML;

            rowArray.push(value);
            //console.log('rowArray.length: '+rowArray.length);
            console.log('value: '+value);
        }


        c = c - 1;
    }
    // Add row to master array
    masterArray.push(rowArray);
    console.log('masterArray.lengh: '+masterArray.length);
}

4 个答案:

答案 0 :(得分:1)

使用childNodes会使其比所需更难,因为它还会选择文本节点。

而是使用一些ES6功能,这些功能可以产生简洁的代码:

&#13;
&#13;
var arr = Array.from(document.querySelectorAll('.gmvcRow'), row =>
    Array.from(row.querySelectorAll('.gmvcCell'), cell => cell.textContent)
);

console.log(arr);
&#13;
<div class="gmvcRow">
  <div class="gmvcCell">firstname</div>
  <div class="gmvcCell">lastname</div>
  <div class="gmvcCell">dob</div>
  <div class="gmvcCell">city</div>
</div>
<div class="gmvcRow">
  <div class="gmvcCell">Helene</div>
  <div class="gmvcCell">Johnson</div>
  <div class="gmvcCell">11/11/1995</div>
  <div class="gmvcCell">Paris</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

1级嵌套的快速样本

var rows = Array.from(document.getElementById('container').querySelectorAll('.gmvcRow'));

const result = rows.map(row => {
    return Array
        .from(row.querySelectorAll('.gmvcCell'))
        .map(cell => cell.innerText);
});

console.log(result);

https://jsfiddle.net/snba2qsf/

毕竟你可以过滤结果以排除空数组

答案 2 :(得分:0)

在您的脚本中:只需在添加到行数组之前检查该值是否为空且未定义。

if(value != null || value != undefined)
{
rowArray.push(value.trim());
}

答案 3 :(得分:0)

以下是我使用querySelectorAll

的答案

var gmvcRowArray = [];
document.querySelectorAll('.gmvcRow').forEach((el, idxRow) =>{
var gmvcCellArray = [];
	el.querySelectorAll('.gmvcCell')
  .forEach((el) =>{
  	gmvcCellArray.push(el.innerText);
  });
  gmvcRowArray.push(gmvcCellArray)
})
console.log(gmvcRowArray)
 <div class="gmvcRow">
      <div class="gmvcCell">Name1</div>
      <div class="gmvcCell">lastname1</div>
      <div class="gmvcCell">dob1</div>
      <div class="gmvcCell">city1</div>
 </div>
 <div class="gmvcRow">
      <div class="gmvcCell">Name2</div>
      <div class="gmvcCell">lastname2</div>
      <div class="gmvcCell">dob2</div>
      <div class="gmvcCell">city2</div>
 </div>
 <div class="gmvcRow"></div>
 <div class="gmvcRow"></div>
 <div class="gmvcRow"></div>
 <div class="gmvcRow"></div>