0

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*/ 
2
  • Please click edit, then [<>] snippet editor and post a minimal reproducible example Commented Apr 16, 2020 at 11:16
  • You can pretty much copy your existing loop Commented Apr 16, 2020 at 11:16

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.