如何使用jQuery在JavaScript中循环嵌套数组对象

假符文

嘿,我正在做一个项目,我似乎无法理解。我想遍历嵌套数组对象“产品”,以便我可以显示所有内容,而不仅仅是最后一个索引。

// jquery getting our json order data from firebase
    $.get("http://localhost:8888/orderslist", (data) => {    


        // i is for the index of the array of orders
        let i = 0;    
        //for each loop through our array list
        $.each(data, function () {
            //console.log(data)
            //console.log(i);
            // is how we arrange the data and show it to the frontpage
            $(`<table id = order_table_layout>
                    <tr>
                        <th>Customer</th>
                        <th>Date</th>
                        <th>Time</th>
                        <th>Total</th>
                        <th>Order</th>
                        <th>Order Status</th>
                    </tr>
                    <tr>
                        <td>${data[i].customer_name}</td>
                        <td>${data[i].date}</td>
                        <td>${data[i].time}</td>
                        <td>${data[i].total} Kr.</td>

                        <td>
                            ${data[i].products[i].name}
                            ${data[i].products[i].price} Kr.
                        </td>

                        <td>

                        </td>
                    </tr>
                </table>`

            ).appendTo("#frontpage_new_ordertable");        
            // counts 1 up for each loop to go through list
            i++;
            //console.log(i);
        });
    });

编辑:我正在使用的json数据的示例如下所示:

[
  {
    id: "4WQITi6aXvQJsKilBMns",
    customer_name: "Susanne",
    date: "22-12-2002",
    time: "12:43:19",
    total: 222,
    products: [
      { name: "product name",  price: 100 },
      { name: "product name2", price: 20  }
    ]
罗里·麦克罗森(Rory McCrossan)

您的代码中有几个问题。首先,您要为data数组中的每个对象创建一个全新的表相反,在表中为每个项目创建一个新要有意义得多

同样,您似乎想遍历子products数组。因此,您需要一个内部循环来为模板文字之外的那些元素创建HTML字符串。

但是,值得注意的是,在您的JS中包含这么多的HTML不是一个好习惯。更好的方法是tr在HTML中有一个隐藏的模板,您可以克隆该模板,使用data数组中的数据进行更新,然后将其追加到表的DOM中tbody

话虽如此,请尝试以下操作:

//$.get("http://localhost:8888/orderslist", (data) => {
  // mock response:
  let data = [{id:"4WQITi6aXvQJsKilBMns",customer_name:"Susanne",date:"22-12-2002",time:"12:43:19",total:222,products:[{name:"product name",price:100},{name:"product name2",price:20}]},{id:"asjdkjk21ijjjew",customer_name:"Foo Bar",date:"10-05-2020",time:"16:46:16",total:68,products:[{name:"Lorem ipsum",price:50},{name:"Fizz buzz",price:18}]}];

  let rows = data.map(item => {
    let $clone = $('#frontpage_new_ordertable tfoot tr').clone();
    $clone.find('.customer-name').text(item.customer_name);
    $clone.find('.date').text(item.date);
    $clone.find('.time').text(item.time);
    $clone.find('.total').text(item.total + ' Kr.');

    let products = item.products.map(prod => `${prod.name}: ${prod.price} Kr.`);
    $clone.find('.products').html(products.join('<br />'));
    return $clone;
  });

  $("#frontpage_new_ordertable tbody").append(rows);
//});
tfoot { 
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="frontpage_new_ordertable">
  <tbody>
    <tr>
      <th>Customer</th>
      <th>Date</th>
      <th>Time</th>
      <th>Total</th>
      <th>Order</th>
      <th>Order Status</th>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td class="customer-name"></td>
      <td class="date"></td>
      <td class="time"></td>
      <td class="total"></td>
      <td class="products"></td>
      <td></td>
    </tr>
  </tfoot>
</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Javascript:使用嵌套 for 循环比较嵌套数组中的元素

如何基于嵌套数组中的值返回对象?(Javascript)

在 javascript 中,如何从 json 数据创建嵌套数组或对象?

循环嵌套数组在 JavaScript 中求和

for 循环:javaScript 中的嵌套数组

使用 jQuery/JavaScript 在 Firebase 中嵌套数组

如何使用 JavaScript 解析嵌套数组中的 JSON 值

如何在对象数组的嵌套数组中获取数组对象javascript

如何使用嵌套循环 JS 从嵌套数组中创建新数组

如何在嵌套数组对象数组的条件下使用 While 循环?

如何使用jQuery遍历嵌套数组

Ansible - 在对象列表中循环嵌套数组?

在ListView React本机中循环嵌套数组对象

如何构造循环以检查嵌套数组中是否不存在对象值

在javascript中如何在具有嵌套数组的对象数组中查找值

对象中的嵌套数组

数组中包含嵌套数组中值的Javascript列表对象

Javascript或lodash:使用嵌套数组的值过滤对象数组

如何在javascript中更改嵌套数组对象键和相同的数组

如何通过在mongo中过滤嵌套数组的嵌套数组来查询对象的嵌套数组

如何从对象数组中的嵌套数组访问特定值?

如何从数组对象中的嵌套数组获取数据

如何在嵌套数组中过滤JSON对象数组

循环遍历嵌套数组并在 JavaScript 中重新排序

在javascript中循环嵌套数组。无法显示

在 JavaScript 中循环遍历 JSON 嵌套数组

使用 Typescript 中的条件对象过滤嵌套数组的数组

如何使用嵌套 for 循环在 JavaScript 中的对象中定位数组的名称和列表

如何在JavaScript中使用带有for循环的嵌套数组