我已经声明了 array
object
变量,它是 ordered_items
,看起来没问题。但现在我如何使用 JavaScript 中的 foreach 循环
检索对象变量内的每个项目。提前致谢。
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)
关于javascript - 如何使用 javascript 在数组对象变量中使用 foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61248836/