<i id='I81vR'><tr id='I81vR'><dt id='I81vR'><q id='I81vR'><span id='I81vR'><b id='I81vR'><form id='I81vR'><ins id='I81vR'></ins><ul id='I81vR'></ul><sub id='I81vR'></sub></form><legend id='I81vR'></legend><bdo id='I81vR'><pre id='I81vR'><center id='I81vR'></center></pre></bdo></b><th id='I81vR'></th></span></q></dt></tr></i><div id='I81vR'><tfoot id='I81vR'></tfoot><dl id='I81vR'><fieldset id='I81vR'></fieldset></dl></div>
    <tfoot id='I81vR'></tfoot>

    <legend id='I81vR'><style id='I81vR'><dir id='I81vR'><q id='I81vR'></q></dir></style></legend>
    • <bdo id='I81vR'></bdo><ul id='I81vR'></ul>

      <small id='I81vR'></small><noframes id='I81vR'>

        循环并拉入一定数量的数据

        时间:2023-10-02
      1. <legend id='j33N5'><style id='j33N5'><dir id='j33N5'><q id='j33N5'></q></dir></style></legend>
          <tbody id='j33N5'></tbody>

        <i id='j33N5'><tr id='j33N5'><dt id='j33N5'><q id='j33N5'><span id='j33N5'><b id='j33N5'><form id='j33N5'><ins id='j33N5'></ins><ul id='j33N5'></ul><sub id='j33N5'></sub></form><legend id='j33N5'></legend><bdo id='j33N5'><pre id='j33N5'><center id='j33N5'></center></pre></bdo></b><th id='j33N5'></th></span></q></dt></tr></i><div id='j33N5'><tfoot id='j33N5'></tfoot><dl id='j33N5'><fieldset id='j33N5'></fieldset></dl></div>
            <tfoot id='j33N5'></tfoot>
            • <bdo id='j33N5'></bdo><ul id='j33N5'></ul>

                  <small id='j33N5'></small><noframes id='j33N5'>

                  本文介绍了循环并拉入一定数量的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找一种更好的方法来遍历 json 对象并拉入一定数量的对象.我只是对如何创建循环有点困惑.现在我正在手动硬编码并使用 [] 获取数据以获取密钥对值.我知道有更好的方法来做到这一点.

                  I'm looking for a better way to loop through a json object and pull in a certain amount of objects. I'm just a little confused on how to create the loop. Right now I'm manually hardcoding and getting the data using [] to get key pair values. I know there is a better way to do this.

                  这是我的代码:

                  fetch("https://api.coinmarketcap.com/v2/ticker/?start=0&limit=200")
                  .then((response) => {return response.json(); })
                  .then((resp => {
                    console.log(resp);
                    let price = resp.data;
                    showPrice(price);
                  }))
                  
                  function showPrice(cryptoPrice) {
                    document.querySelector(".results").innerHTML = `
                     <div class="container text-center">
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[1].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[1].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[1].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[2].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[2].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[2].symbol}</span>
                       </div>
                        
                       <div class="col-4">
                         <h1>${cryptoPrice[52].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[52].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[52].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[74].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[74].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[74].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[109].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[109].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[109].symbol}</span>
                       </div>
                       
                       <div class="col-4">
                         <h1>${cryptoPrice[118].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[118].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[118].symbol}</span>
                       </div>
                       
                       <div class="col-4">
                         <h1>${cryptoPrice[131].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[131].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[131].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[213].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[213].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[213].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[291].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[291].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[291].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[328].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[328].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[328].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[372].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[372].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[372].symbol}</span>
                       </div>
                  
                       <div class="col-4">
                         <h1>${cryptoPrice[463].name}</h1>
                         <p><b>Price:</b> <span class="dollar-amount">${cryptoPrice[463].quotes.USD.price}</span></p>
                         <span><b>Symbol:</b> ${cryptoPrice[463].symbol}</span>
                       </div>
                  
                     </div>
                  `;
                  }

                  .container {
                    display: flex;
                    flex-wrap: wrap;
                    justify-content: space-between;
                    flex-direction: row;
                    max-width: 1080px;
                    padding-left: 15px;
                    padding-right: 15px;
                    margin-left: auto;
                    margin-right: auto;
                  }
                  
                  .text-center {
                    text-align: center;
                  }
                  
                  .col-4 {
                    flex: 0 0 33.3%;
                    margin-bottom: 1em;
                  }
                  
                  h1 {
                    margin-bottom: 0;
                  }
                  
                  .dollar-amount {
                    color: green;
                  }
                  
                  @media(max-width: 1000px) {
                    .col-4 {
                      flex: 0 0 49.9%;
                    }
                  }
                  
                  @media(max-width: 678px) {
                    .col-4 {
                      flex: 0 0 100%;
                    }
                  }

                  <div class="results"></div>

                  如果您想获得更好的外观,这里是我正在开发的 codepen 的链接.https://codepen.io/Brushel/pen/mjrqXr

                  Here is a link to the codepen I'm working on if you'd like to get a better look. https://codepen.io/Brushel/pen/mjrqXr

                  推荐答案

                  https://codepen.io/dennisseah/pen/pZEpQo

                  fetch("https://api.coinmarketcap.com/v2/ticker/?start=0&limit=200")
                    .then(response => {
                      return response.json();
                    })
                    .then(resp => {
                      var oData = Object.getOwnPropertyNames(resp.data).slice(0, 20).map(function(i) {
                        return resp.data[i];
                      });
                      showPrice(oData);
                    });
                  
                  function showPrice(cryptoPrice) {
                    var divs = cryptoPrice
                      .map(function(price) {
                        return `<div class="col-4">
                        <h1>${price.name}</h1>
                        <p><b>Price:</b> <span class="dollar-amount">${
                          price.quotes.USD.price
                        }</span></p>
                        <span><b>Symbol:</b> ${price.symbol}</span>
                      </div>`;
                      })
                      .join("");
                    document.querySelector(".results").innerHTML = `
                        <div class="container text-center">${divs}</div>`;
                  }
                  

                  这篇关于循环并拉入一定数量的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以将 js 变量发送到谷歌表格?(不是表格数据) 下一篇:从 fetch 请求中获取 html

                  相关文章

                • <i id='TYFBJ'><tr id='TYFBJ'><dt id='TYFBJ'><q id='TYFBJ'><span id='TYFBJ'><b id='TYFBJ'><form id='TYFBJ'><ins id='TYFBJ'></ins><ul id='TYFBJ'></ul><sub id='TYFBJ'></sub></form><legend id='TYFBJ'></legend><bdo id='TYFBJ'><pre id='TYFBJ'><center id='TYFBJ'></center></pre></bdo></b><th id='TYFBJ'></th></span></q></dt></tr></i><div id='TYFBJ'><tfoot id='TYFBJ'></tfoot><dl id='TYFBJ'><fieldset id='TYFBJ'></fieldset></dl></div>

                    <legend id='TYFBJ'><style id='TYFBJ'><dir id='TYFBJ'><q id='TYFBJ'></q></dir></style></legend>

                        <bdo id='TYFBJ'></bdo><ul id='TYFBJ'></ul>
                    1. <small id='TYFBJ'></small><noframes id='TYFBJ'>

                      <tfoot id='TYFBJ'></tfoot>