我有这样的代码,我试图从pokeapi打印'stats‘数组的内容,但是当函数在浏览器中被调用时,我只得到数组的最后一个元素。
如何将其全部放在HTML端?谢谢!
main.js:
fetch(apiURL).then(response => {
response.json();
}
.then(data => {
let array
for(let i=0; i<data.stats.length; i++ ) {
array = data.stats[i].stat.name;
console.log(array); // <- I have all the array content here
}
pokemonFoundStats = array;
console.log(pokemonFoundStats); // <- I just get the last element from the array
pokemonStats(pokemonFoundStats);
}) };
// function for printing in the html
const pokemonStats = (pokemonStatsInfo) => {
const allStats = document.getElementById("poke-screen-id");
allStats.value = ` Stats: ${pokemonStatsInfo}`;
};HTML:我尝试打印数组内容的地方
<input id="poke-screen-id" type="text" placeholder="stats">发布于 2022-03-29 10:59:22
请在下面找到密码。或者检查代码沙箱这里的虚拟数据。https://codesandbox.io/s/javascript-forked-ch6bky?file=/index.js
fetch(apiURL)
.then((response) => response.json())
.then((data) => {
let array = [];
for(let i=0; i<data.stats.length; i++ ) {
array.push(data.stats[i].stat.name;);
// console.log(array);
pokemonStats(array);
}
});
// function for printing in the html
const pokemonStats = (pokemonStatsInfo) => {
const allStats = document.getElementById("poke-screen-id");
allStats.value = `Stats: ${pokemonStatsInfo}`;
};https://stackoverflow.com/questions/71628577
复制相似问题