如何使用javascript在数组对象变量中使用foreach循环?

问题描述 投票:0回答:1

我已宣布 array object 变量,即 ordered_items 并且似乎没有问题。但是,现在我如何使用 foreach loop 在javascript中。先谢谢你。

        var ordered_items = [];

        for(var x = 0; x < prev_tbl_rows.length; x++){

            var product = $(prev_tbl_rows[x]).find("td.one_prod_name").text();
            var quantity = $(prev_tbl_rows[x]).find("td.one_qty").text();
            var price = $(prev_tbl_rows[x]).find("td.oneprice").text();
            var subtotal = $(prev_tbl_rows[x]).find("td.oneSubtotal").text();

            ordered_items.push({
                product: product,
                quantity: quantity,
                price: price,
                subtotal: subtotal,
            });

        }

        console.log(ordered_items);

       //I don't have an idea how to retrieve each item inside this variable /*ordered_items*/ 
javascript arrays syntax
1个回答
1
投票

你是说这个吗?

let ordered_items = [];
$("someSelectorForPrevRows").each(function() {
  $row = $(this);
  ordered_items.push({
    product: $row.find("td.one_prod_name").text(),
    quantity: +$row.find("td.one_qty").text(),
    price: +$row.find("td.oneprice").text(),
    subtotal: +$row.find("td.oneSubtotal").text()
  })
})
ordered_items.forEach(item => console.log(item.product, item.quantity, item.price, item.subtotal)
© www.soinside.com 2019 - 2024. All rights reserved.