0

I got a situation like this:

var orders = [ 
{ID: "sampleID1", order: "string describing the first order"},
{ID: "sampleID2", order: "string describing the second order"}
];

const bill = document.querySelector("#ordersListed");

for(var x in orders) {
  bill.innerHTML += orders[x].order;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="ordersListed"></div>
</body>
</html>

Each time I click a button, an item gets added to the array.
I've been trying to loop through the array to print each order in a HTML element.

Should show up in the div as:

string describing the first order
string describing the second order

My results:

first order
first order
second order

3
  • 2
    What is the expected output? Commented Mar 21, 2018 at 10:34
  • 1
    Please also add HTML and SCRIPT to the snippet to make a minimal reproducible example Commented Mar 21, 2018 at 10:35
  • I'd recommend creating real HTML Nodes rather than just appending strings because it gives you so much more control and options with the created list. Commented Mar 21, 2018 at 11:05

3 Answers 3

2

Just adding a <br> to the innerHTML in each iteration of Array's forEach() will do the trick:

var orders = [ 
  {ID: "sampleID1", order: "string describing the first order"},
  {ID: "sampleID2", order: "string describing the second order"}
];

const bill = document.querySelector("#ordersListed");

orders.forEach(o => bill.innerHTML += o.order + '<br>');
<div id="ordersListed"></div>

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

Comments

1

for...in is for iterating over Object properties, not for Array. Use for...of:

var orders = [{ ID: "sampleID1", order: "string describing the first order" }, { ID: "sampleID2", order: "string describing the second order" }]

for (order of orders) {
  let para = document.createElement('p')
  para.textContent = order.order
  ordersListed.appendChild(para)
}
#ordersListed {
  background: #f8f8f8;
  border: 4px dashed #ddd;
  padding: 5px 15px;
}
<div id="ordersListed"></div>

Note: I prefer to create real DOM Nodes so I can easily add attributes and attach event handlers. In the second example I show adding title and alerting the order ID on click:

var orders = [{ ID: "sampleID1", order: "string describing the first order" }, { ID: "sampleID2", order: "string describing the second order" }]

for (order of orders) {
  let para = document.createElement('p')
  para.textContent = order.order
  para.title = order.ID
  para.addEventListener('click', function() { alert(this.title) })
  ordersListed.appendChild(para)
}
#ordersListed {
  background: #f8f8f8;
  border: 4px dashed #ddd;
  padding: 5px 15px;
}

#ordersListed p {
  cursor: pointer;
}
<div id="ordersListed"></div>

Comments

0

How about:

var orders = [ 
  {ID: "sampleID1", order: "string describing the first order"},
  {ID: "sampleID2", order: "string describing the second order"}
];

for (var i = 0; i < orders.length; i++ ) {
  orderListContainer.textContent += orders[i]['order'];
}

As far as I can see you were appending the array element instead of the correct property to the array element.

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.