我已经声明了array
object
变量,它是ordered_items
,似乎很好。但是现在我该如何使用javascript中的foreach loop
检索对象变量中的每个项目。提前致谢。
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,
});
}
您是说这个吗?
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)