3 回答

TA貢獻1848條經驗 獲得超2個贊
您可以使用這個循環:
var arr_nos = {};
$('#table tbody tr').each(function( index,elem ) {
var parent_no = $(elem).find('.parent_no').html();
var child_id = $(elem).find('.child_id ').html();
if(parent_no != '') {
if(!arr_nos[parent_no]) arr_nos[parent_no] = [];
arr_nos[parent_no].push(child_id);
}
});
console.log(arr_nos);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
<tbody>
<tr>
<th class='parent_no'>1</th>
<th class='child_id'>11</th>
</tr>
<tr>
<th class='parent_no'>2</th>
<th class='child_id'>22</th>
</tr>
<tr>
<th class='parent_no'>3</th>
<th class='child_id'>33</th>
</tr>
<tr>
<th class='parent_no'>2</th>
<th class='child_id'>444</th>
</tr>
</tbody>
</table>

TA貢獻1712條經驗 獲得超3個贊
代替:
var arr_nos = [] arr_nos.push(parent_no, child)
做:
let arr_nos = {} arr_nos[parent_no] = [...arr_nos[parent_no], child]

TA貢獻1773條經驗 獲得超3個贊
要獲取示例中的數組對象,您必須執行以下操作
arr_nos[parent_no] = child
并且必須將對象聲明為
arr_nos = {}
代替
arr_nos = []
但這是數組的對象,而不是對象的數組。要么你的例子錯了,要么你的定義錯了
我不確定如何獲取您的具體示例{123456: [123, 124, 125], 123457: [345, 346, 347]}
。您必須指定必須制定什么標準來決定該對象的鍵是什么。
添加回答
舉報