Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将表转换为多维数组_Javascript_Jquery_Arrays_Html Table - Fatal编程技术网

Javascript 将表转换为多维数组

Javascript 将表转换为多维数组,javascript,jquery,arrays,html-table,Javascript,Jquery,Arrays,Html Table,我有点像一个JavaScriptNoob,所以解决这个问题的方法可能比我意识到的更基本。我试图从这个表中获取数据,并将其显示在一个数组中。该表如下所示: <table id="myTable"> <col> <thead> <tr> <th>Date</th> <th>Results1</th

我有点像一个JavaScriptNoob,所以解决这个问题的方法可能比我意识到的更基本。我试图从这个表中获取数据,并将其显示在一个数组中。该表如下所示:

<table id="myTable">
     <col>
        <thead>
             <tr>
                <th>Date</th>
                <th>Results1</th>
                <th>Results2</th>
            </tr>
         </thead>
         <tbody>
             <tr>
                <td>Jan-00</td>
                <td>9</td>
                    <td>10</td>
             </tr>
             <tr>
                <td>Feb-00</td>
                <td>92</td>
                <td>64</td>
             </tr>
在console.log测试中,变量line1现在显示其保留键和值,如下所示:Date:Jan 00,Results1:9,Results2:10

这很好,但我确实需要将这些值以以下格式放入新数组:

var new = [[Date, Results1], [Date, Results2], etc...];
如果我重复了一个问题,但找不到我想要的东西,请道歉。非常感谢任何人能提供的帮助

这应该可以做到:

var ary = [];
$('tr').each(function() {
    date = $('td:first', this).text();
    $('td:gt(0)', this).each(function() {
        ary.push([date, $(this).text()]);
    });
});
看看 我建议使用tableObject.rows来获取包含单元格的行数组。然后使用rows.cells获取所有单元格元素。 它可能看起来像

   var table =document.getElementById("myTable")
  var foo = new array(table.rows.length);
  for(var j =0; j<foo.length; j++)
   foo[j]=foo[j].cells;
var table=document.getElementById(“myTable”)
var foo=新数组(table.rows.length);

for(var j=0;jDon)在
中不使用
for…来迭代数组,也不需要使用
$。each()
在该循环中。谢谢!这很有效,尽管我仍在解包它的工作原理。非常感谢您的帮助。基本上,它遍历表中的行,将第一个单元格的值存储为日期,然后遍历行中的剩余单元格,在运行时将日期/值对添加到数组中。谢谢GreenGiant。我将在下面对此进行研究这也是一个可能更有效的解决方案。
   var table =document.getElementById("myTable")
  var foo = new array(table.rows.length);
  for(var j =0; j<foo.length; j++)
   foo[j]=foo[j].cells;