I’m in a pickle because my function is not displaying anything in an array.
I know there are 3 objects in an array (taken from an API):
PC
macOS
PlayStation
but when I try this code:
function platforms() {
let platform = [];
platform = details.platforms;
for (let i = 0; i < platform.length; i++) {
console.log(platform[i].platform.name);
platform[i].platform.name;
}
}
console.log works just fine but another line – platform[i].platform.name; – is showing me UNDEFINED.
Everything else works just fine. I can take titles, screenshots, score etc but not arrays unfortunately 🙁
And if it helps here is my slightly bigger piece of code
function displayGameDetails(details) {
function platforms() {
let platform = [];
platform = details.platforms;
for (let i = 0; i < platform.length; i++) {
console.log(platform[i].platform.name);
platform[i].platform.name;
}
}
resultGrid.innerHTML = `
<div class="box">
<p class="fourPara">${platforms()}</p>
</div >
`;
}
2
Answers
It works! Big thanks to Mr. Polywhirl and David. Like David said I had to use RETURN keyword and Mr.Polywhirl pointed me in the right direction, the map() method!
(noob)SOLUTION
You could simplify this greatly: