简体   繁体   English

从列中获取所有值,并将它们放入javascript中的数组

[英]Get all values from column and put them into an array in javascript

I need to get all the values from a column (except header value of course), then populate an array with the column values. 我需要从列中获取所有值(当然,标题值除外),然后用列值填充数组。 I am restricted to native JavaScript. 我仅限于本机JavaScript。

This is a table header: 这是一个表头:

<table id="results" width="360" border="1">
    <thead>
      <tr>
        <th scope="col" width="120">Date Created</th>
        <th scope="col" width="120">Name</th>
        <th scope="col" width="120">Tests</th>
      </tr>
    </thead>
  </table>

And this is how the rows are created: 这是创建行的方式:

      var table = document.getElementById("results");
      var name = document.getElementById("tbName").value;
      var elements = document.getElementsByTagName("select")
      var testArray = [];
      var test;
      for(var i=0; i < elements.length ; i++)
      {
        testArray[i] = elements[i].value;
      }
      test = testArray.join(',');

      var today = new Date();
      var dd = today.getDate();
      var mm = today.getMonth()+1; 
      var yyyy = today.getFullYear();

      if(dd<10) {
          dd='0'+dd
      } 

      if(mm<10) {
          mm='0'+mm
      } 

      today = mm+'/'+dd+'/'+yyyy;

      //In the first step using InsertRow function you are creating a first row i.e tr 
      var row = table.insertRow(table.rows.length);

      //In the second step you create a cell i.e td dynamically
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      var cell3 = row.insertCell(2);

      // Here in step 3 you can add data to your cells from the modal popup inputs(According to your logic)
      cell1.innerHTML = today;
      cell2.innerHTML = name;
      cell3.innerHTML = test;

Again I would like to populate an array with the values in the column Name . 同样,我想用Name列中的值填充数组。

There you go. 妳去 I think the code is so descriptive it doesn't need further explanation :) 我认为代码的描述性很强,无需进一步说明:)

var firstCells = document.querySelectorAll('td:nth-child(2)');
var cellValues = [];
firstCells.forEach(function(singleCell) {
  cellValues.push(singleCell.innerText);
});
console.log(cellValues);
 //get parent element
 parent=document.getElementById("name");
//get all tags inside this element
 childs=parent.getElementsByTagName("td or sth like that");
//create an empty array
names=[];
//loop through childs
childs.forEach(child){
     //add child balue to array
     names.push(child.value);
});

 var table = document.getElementById("result"); //ur table id var rows = table.getElementsByTagName("tr"); var output = []; for(var i=0;i<rows.length;i++) { output.push(rows[i].getElementsByTagName("td")[1].innerHTML);//instead of 1 pass column index } alert(output); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript量角器从列中获取所有值并将其求和 - Javascript Protractor Get all values from a column and sum them 如何获取列中的值并将它们放入数组中? 谷歌表格 - How to get values in a column and put them into an array? Google Sheets 有没有办法从数组中获取括号,然后将其中的所有元素放入数组中 - Is there a way to get parenthesis from an array, and then put all the elements in them into an array 将输入字段中的所有值放入数组中并对其进行排序 - Put all values from input fields into an array and sort them 如何将所有值放入数组并使用javascript获取它们 - How to push all values into array and get them with javascript 如何从数组中的 object 中获取值并将它们放入列中的数组中? - how do i take values from an object inside an array and put them inside a array in column? 从javascript数组获取所有值 - get all values from javascript array jQuery:从输入数组中获取所有值并将它们计算为变量 - jQuery: get all values from input array and calculate them to variable 使用JavaScript从html文档中的链接获取值并将它们存储在数组中 - get values from links in html document with JavaScript and store them in an array (Javascript)如何使用布尔值从数组中查找唯一值并将其放入另一个值? - (Javascript) How to find unique values from an array and put them into another using booleans?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM