如何防止 .map 从嵌套 table 创建重复结果?
How to prevent .map from creating duplicate results from nested table?
我正在尝试将 map 方法应用于动态创建的 table,以便 select 具有 class = book_info 的跨度元素。由于嵌套 table,结果数组包含重复数据,但我不确定为什么 - 有人可以解释一下吗?将 map 方法应用于此 table 以获得所需数据的正确方法是什么?或者我应该改用 each 方法?
var $table = $("#table_0");
var textbook_data = $table.find('tbody tr').map(function() {
return $(this).find("span.book_info").html();
}).toArray();
console.log(textbook_data); //view results
//display results
var output = "";
textbook_data.forEach(function(item) {
output += "<p>" + item + "</p>";
});
$("#array_results").empty().append(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table_0">
<tbody>
<tr>
<td><span class="book_info"><img src="https://covers.openlibrary.org/b/id/10131432-M.jpg?default=false"></span></td>
<td valign="middle">
<table id="info">
<tbody>
<tr>
<td><b>Title: </b><span class="book_info"><b>Advanced Accounting (13th Edition)</b></span></td>
</tr>
<tr>
<td valign="top"><b>Publisher: </b><span class="book_info">Pearson</span></td>
</tr>
<tr>
<td valign="top" style="padding:0px;">
<table id="authors">
<tbody>
<tr>
<td valign="top"><b>Author(s): </b></td>
<td valign="top" style="padding-left:0px;"><span class="book_info">Bruce Bettinghaus<br>Joseph H. Anthony<br>Floyd A. Beams<br>Smith, Kenneth<br></span></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<br>
<div id="array_results"></div>
感谢@charlietfl
$("#table_0 > tbody > tr").each(function() {
textbook_data = $(this).find("td span.book_info").map(function() {
return $(this).html();
}).toArray();
});
console.log(textbook_data);
我正在尝试将 map 方法应用于动态创建的 table,以便 select 具有 class = book_info 的跨度元素。由于嵌套 table,结果数组包含重复数据,但我不确定为什么 - 有人可以解释一下吗?将 map 方法应用于此 table 以获得所需数据的正确方法是什么?或者我应该改用 each 方法?
var $table = $("#table_0");
var textbook_data = $table.find('tbody tr').map(function() {
return $(this).find("span.book_info").html();
}).toArray();
console.log(textbook_data); //view results
//display results
var output = "";
textbook_data.forEach(function(item) {
output += "<p>" + item + "</p>";
});
$("#array_results").empty().append(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table_0">
<tbody>
<tr>
<td><span class="book_info"><img src="https://covers.openlibrary.org/b/id/10131432-M.jpg?default=false"></span></td>
<td valign="middle">
<table id="info">
<tbody>
<tr>
<td><b>Title: </b><span class="book_info"><b>Advanced Accounting (13th Edition)</b></span></td>
</tr>
<tr>
<td valign="top"><b>Publisher: </b><span class="book_info">Pearson</span></td>
</tr>
<tr>
<td valign="top" style="padding:0px;">
<table id="authors">
<tbody>
<tr>
<td valign="top"><b>Author(s): </b></td>
<td valign="top" style="padding-left:0px;"><span class="book_info">Bruce Bettinghaus<br>Joseph H. Anthony<br>Floyd A. Beams<br>Smith, Kenneth<br></span></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<br>
<div id="array_results"></div>
感谢@charlietfl
$("#table_0 > tbody > tr").each(function() {
textbook_data = $(this).find("td span.book_info").map(function() {
return $(this).html();
}).toArray();
});
console.log(textbook_data);