首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打印所有数组(pokeapi)?

如何打印所有数组(pokeapi)?
EN

Stack Overflow用户
提问于 2022-03-26 13:46:41
回答 1查看 115关注 0票数 0

我有这样的代码,我试图从pokeapi打印'stats‘数组的内容,但是当函数在浏览器中被调用时,我只得到数组的最后一个元素。

如何将其全部放在HTML端?谢谢!

main.js:

代码语言:javascript
复制
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:我尝试打印数组内容的地方

代码语言:javascript
复制
<input id="poke-screen-id" type="text" placeholder="stats">
EN

回答 1

Stack Overflow用户

发布于 2022-03-29 10:59:22

请在下面找到密码。或者检查代码沙箱这里的虚拟数据。https://codesandbox.io/s/javascript-forked-ch6bky?file=/index.js

代码语言:javascript
复制
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}`;
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71628577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档