将表放入数组 [英] Put table in array
本文介绍了将表放入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,我有一张桌子
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
我有一个要用此数据填充的数组:
And I have an array to be filled with this data:
var params = {
"fruit":"apple",
"test": "Nope",
"values": [
[Here,Must ,Come the header ],
[Here,must come ,the first table data],
[Here must,comt the ,second tabel data]
],
}
这仅是1个表的示例 一些表包含更多行.
And this is just an example of 1 table some tables contain more rows.
如何确保整个数据都在数组中?
How can I make sure the whole data comes in the array ?
推荐答案
您需要嵌套循环才能获得所需的结果.参见下面的代码段
You need nested loops to achieve the required result. See the below snippet
var params = {
"fruit":"apple",
"test": "Nope",
"values": [
],
}
$(function(){
$('button').click(function() {
$("#mytable tr").each(function(index, item){
var temp = []
if(index == 0) {
$(item).find('th').each(function(idx, col){
temp.push($(col).text());
})
}
$(item).find('td').each(function(idx, col){
temp.push($(col).text());
})
params.values.push(temp)
})
console.log(params);
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mytable">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
<button>Click</button>
这篇关于将表放入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文