我創建了一個函式,用于回傳位于以下api網站上的隨機用戶的登錄名(https://api.github.com/users)
<script>
fetch('https://api.github.com/users').then(function(response) {
回應.json().then(function(users){
document.write("Highighred Github users"/span>)
users.forEach(function(user){
for (let i=0; i< 8; i ) {
document.write('<br> ')
document.write((users[Math. floor(Math.random() *users.length) ].login)
}
});
});
}).catch(err => console.error(err) )。
</script>
目前,它確實從該檔案中隨機回傳登錄名,但是,我希望只從這個串列中隨機選擇8個用戶。我的嘗試可以在for回圈中看到i < 8,但是它列印出的資料遠遠多于8(確切地說,是240)
我怎樣才能讓它隨機回傳8呢? 例如
ezmobius
モークロードナ
kevwil
馮小剛
軌交
bmizerany
atmos
fanvsfan
uj5u.com熱心網友回復:
請洗掉無用的回圈,我已經評論了users.forEach(function(user){
span class="hljs-title function_">fetch('https://api. github.com/users').then(function(response) {
回應.json().then(function(users){
document.write("Highighred Github users"/span>)
// users.forEach(function(user){ )
for (let i=0; i< 8; i ) {
document.write('<br> ')
document.write((users[Math. floor(Math.random() *users.length) ].login)
}
});
// });.
}).catch(err => console. error(err));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/333757.html
標籤:
上一篇:實作從url獲取路徑變數的投票者
下一篇:考慮到std::cout是一個初始化的物件,為什么visualstudio在除錯器中設定觀察時"不能識別其識別符號"?
