我已宣布 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*/
你是说这个吗?
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)