为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信