【问题标题】:How to display all the 'name' values in this json array [duplicate]如何显示此json数组中的所有“名称”值[重复]
【发布时间】:2020-08-26 09:58:26
【问题描述】:
let url = 'api url';

const players = [];
fetch(url)
.then(response => response.json())
 .then(data => players.push(data.included));

console.log(players);

我正在使用 api 来查看服务器上的玩家。我的输出:

https://imgshare.io/image/NyvUN5

我想显示所有当前玩家的所有名称值。 此数组中的对象数量可以随时不同。

【问题讨论】:

标签: javascript arrays json api object


【解决方案1】:

遍历玩家数组,然后访问每个玩家的属性:

for (var player in players) {
    console.log(player.attributes.name);
}

【讨论】:

    【解决方案2】:
    let url = 'api url';
    
    const players = [];
    fetch(url)
    .then(response => response.json())
    .then(data => players = data.map(v=>v.attributes.name));
    

    我可能会失败,因为 imgshare 而不是 copypast json。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 2016-04-06
      相关资源
      最近更新 更多