问题描述
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
我想显示所有当前播放器的所有名称值。 该数组中的对象数量可以随时不同。
解决方法
浏览玩家数组,然后访问每个玩家的属性:
for (var player in players) {
console.log(player.attributes.name);
}
,
let url = 'api url';
const players = [];
fetch(url)
.then(response => response.json())
.then(data => players = data.map(v=>v.attributes.name));
我会失败,因为imgshare代替了copypast json。