I have one array object
I want loop that with entires() method and display all score
My code is:
const obj = [
{ name: "a", score: 5 },
{ name: "b",score: 8 },
{ name: "c", score: 10 },
{ name: "d",score: 19 }
];
const a = obj.entries();
Array.from(a).forEach((element) => {
document.getElementById("p2").innerHTML += element.score;
});
<p id="p2"></p>
but got undefinedundefinedundefinedundefined what is solution?
You can try this code, it not recommended to use .innerHTML to edit element html, it's better to append an element inside :
const obj = [
{ name: "a", score: 5 },
{ name: "b",score: 8 },
{ name: "c", score: 10 },
{ name: "d",score: 19 }
];
const container = document.querySelector('#p2')
for(let el of obj) {
let p = document.createElement('p');
p.innerText = `${el.name} => ${el.score}`;
container.appendChild(p);
}
I changed the output a little bit to make it clearer for bebug, of course you can put what ever you want inside the .innerText ;)