I have declared array object variable which is ordered_items and it seems to be fine. But now how can I retrieve every item inside the object variable using foreach loop in javascript. Thanks in advance.
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*/
asked Apr 16, 2020 at 11:13
Jesper Martinez
6415 silver badges16 bronze badges
1 Answer 1
You mean this?
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)
answered Apr 16, 2020 at 11:21
mplungjan
180k29 gold badges183 silver badges246 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
[<>]snippet editor and post a minimal reproducible example