0
    </div>
    <script>
        let list = [];
        let ob={};
        for(i=0; i<100; i++){
            list[i] = {
                name: i,
                text: "text"
            }  
            //document.getElementById('results').innerHTML += list[i].name + list[i].text + '<br/>'
        }
        console.log(list)
        var lising = document.getElementById('results');
        
        lising.innerHTML = list
        </script>
</body>

in above code i can print the array inside the loop but when i try to print it outside, it prints [object object]

cshendye
  • 23
  • 4

1 Answers1

0

It is needed to put string value to innerHTML and now, you put Array object value in innerHTML attribute. Converting the list array to string and assign will work.

let list = [];
let ob = {};
for (i = 0; i < 100; i ++) {
  list[i] = {
    name: i,
    text: 'text'
  };
}
console.log(list);
document.getElementById("demo").innerHTML = list.map(({name, text}) => `${name} ${text}`).join("<br/>");
Derek Wang
  • 10,098
  • 4
  • 18
  • 39