简体   繁体   中英

Displaying every item of an array as a new line in HTML

So I have an array called "comments"

comments: ["Comment1","Comment2","Comment3"]

Now I want to display every item as a new line in HTML

Other stuff from my object I display like this:

<p><%= singlegif.get("title") %></p>

But obviously putting "comments" in the above code displays them all next to eachother.. Now how do I display one/line?

Javascript array prototype has a function forEach(callback(element, index, array)) which executes the provided callback once for each element of the array with an assigned value. It is not invoked for indexes which have been deleted or which have been initialized to undefined.

var commentsContainer = document.getElementById("demo");
comments: ["Comment1","Comment2","Comment3"];
comments.forEach(function(element, index){
   // create paragraph for each comment
   var elementForComment = document.createElement('p');

   // set text of paragraph
   elementForComment.innerText = element;

   // add paragraph into comments holder.
   commentsContainer.appendChild(elementForComment);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM