如何用JavaScript限制IPSW.ME API仅返回前两项固件数据?
仅获取IPSW.ME API响应中前两项固件数据的修改方案
你只需要在处理固件数组时添加slice(0, 2)方法截取前两项数据,就能实现需求。修改后的完整代码如下:
function fetchData() { fetch('https://api.ipsw.me/v4/device/iPad8,8') .then((response) => { return response.json(); }) .then(data => { console.log(data); // 关键修改:用slice(0,2)截取前两项固件数据 const html = data.firmwares.slice(0, 2).map(info => { return ` <div class="ipsw-info"> <table id='ipsw-td'> <tr> <td><span class="ios-data">Device:</span></td> <td>iPad Pro 3 (12.9-inch, Cellular, 1TB Model) (${info.identifier})</td> </tr> <tr> <td><span class="ios-data">Firmware Version:</span></td> <td>iOS ${info.version} ( Build ${info.buildid})</td> </tr> <tr> <td><span class="ios-data">Released Date:</span></td> <td> ${info.releasedate}</td> </tr> <tr> <td><span class="ios-data">File MD5sum:</span> </td> <td><code>${info.md5sum}</code></td> </tr> <tr> <td><span class="ios-data">File SHA1sum:</span> </td> <td><code>${info.sha1sum}</code></td> </tr> <tr> <td><span class="ios-data">Signed IPSW:</span></td> <td> ${info.signed}</td> </tr> </table> <center> <div class='dlBox'> <!--[ Change data-text='...' atribute to add new file type ]--> <span class='fT' data-text='IPSW'></span> <div class='fN'> <!--[ File name ]--> <span> Download for ${info.identifier}</span> <span class='fS'>Size: ${info.filesize} bytes</span> </div> <!--[ Download link (change href='...' atribute to add link download) ]--> <a class='button' aria-label='Download' href='${info.url}' rel='noreferrer' target='_blank'><i class='icon dl'></i></a> </div> </center> <h4 style='color:blue; text-align:center'> Can't Download? Copy link from below, paste in a new tab to download</h4> <div class='btnF'> <input type="text" value="${info.url}" id="myInput" style='width:70%'> <button class='button ln' onclick="myFunction()" onmouseout="outFunc()"><span><svg class='line' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><g><rect x='5.54615' y='5.54615' width='16.45385' height='16.45385' rx='4'></rect><path d='M171.33311,181.3216v-8.45385a4,4,0,0,1,4-4H183.787' transform='translate(-169.33311 -166.86775)'></path></g></svg></span> </button> </div> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-0000000000" data-ad-slot="2566286925"></ins> `; }) .join(); console.log(html); document.querySelector('#ipsw-info').insertAdjacentHTML('afterbegin', html); }); } fetchData()
关键修改说明
data.firmwares.slice(0, 2):JavaScript的slice方法会从原数组中截取指定范围的元素并返回新数组,这里0是起始索引(对应第一项),2是结束索引(不包含该索引,因此实际取前两项)。- 后续的
map方法只会遍历截取后的新数组,最终只生成前两项固件数据的HTML内容。
内容的提问来源于stack exchange,提问作者Mobiprox Man
相关产品推荐
相关产品推荐

