将javascript数组数据附加到表td中

每个ID重复两次。.我不想重复每个ID

let t_p_data = [
    {
        id: 11001
    },
    {
        id: 11002
    },
];
let paymentsData = $('.payments-data');
paymentsList = paymentsData.innerHTML;
for (i=0; i<t_p_data.length; i++) {
    for (let p_data of t_p_data) {
        paymentsList += ` <tr><td>${p_data.id}</td></tr>  ` }
}paymentsData.append(paymentsList);    
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table><tbody class="payments-data"></tbody></table>
评论
Laura
Laura
for (i=0; i<t_p_data.length; i++) {

    for (let p_data of **t_p_data[i]**) {

        paymentsList += ` <tr><td>${p_data.id}</td></tr>  ` }
}paymentsData.append(paymentsList);  

在for..of循环中,您引用的是数组,因此它为每个元素输出两倍的id值。如果只需要打印.id属性,请删除其中一个循环。

点赞
评论
rut
rut

为什么需要2个循环?只需删除其中之一

let t_p_data = [
    {
        id: 11001
    },
    {
        id: 11002
    },
];
let paymentsData = $('.payments-data');
paymentsList = paymentsData.innerHTML;

for (let p_data of t_p_data) {
  paymentsList += ` <tr><td>${p_data.id}</td></tr>  ` 
}
paymentsData.append(paymentsList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table><tbody class="payments-data"></tbody></table>
点赞
评论