亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Fetch 調用中迭代數組

在 Fetch 調用中迭代數組

Qyouu 2023-07-06 10:14:17
我有一個像這樣的 JSON 對象:[{"user": "poetry2", "following": ["Moderator", "shopaholic3000"]}]我正在使用這樣的 Fetch API:    fetch (`/profile/${username}/following`)    .then(response => response.json())    .then(profiles => {        profiles.forEach(function(profile){            profileDisplay = document.createElement('button');            profileDisplay.className = "list-group-item list-group-item-action";            profileDisplay.innerHTML = `            ${profile.following}`;            listFollowing.appendChild(profileDisplay);        })    })現在,它在同一個按鈕中顯示以下用戶,如下所示:<button class="list-group-item list-group-item-action">            Moderator,shopaholic3000</button>如何修改 Fetch 調用以在單獨的按鈕中顯示以下每個用戶。更像這樣的東西:<button class="list-group-item list-group-item-action">            Moderator</button><button class="list-group-item list-group-item-action">            shopaholic3000</button>
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

因此,您需要執行第二次循環來循環下面的數組


profiles.forEach(function(profile){

  profile.following.forEach(name => {

    const profileDisplay = document.createElement('button');

    profileDisplay.innerHTML = `${name}`;

    ....

  });

});


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號