gpt4 book ai didi

javascript - 使用带有用户输入的 vanilla js 从 (poke) API 中对 API 数据进行排序

转载 作者:行者123 更新时间:2023-12-01 16:10:35 25 4
gpt4 key购买 nike

嘿,每个人都找不到这个,想知道是否有人可以指出我正确的方向?这是我正在做的作业,我必须使用 API 并显示数据。我还必须进行排序。

我正在使用 poke api,因为它是免费的。我能够毫无问题地显示数据,并希望通过下拉栏或搜索栏添加排序。不确定哪个最好。例如通过“类型”,如草、水、火或其他什么。

抱歉,我对 js 还是很陌生。

我试过:

const grass = pokemon.filter(pokemon => pokemon.type === 'grass') 
if(pokemon.type === 'grass'){
return true;
}

但是当我这样做时,我不断从 api 收到 400 错误。这是我的完整脚本:

const pokedex = document.getElementById('pokedex');

const fetchPokemon = () => {
const promises = [];
for (let i = 1; i <= 150; i++) {
const url = `https://pokeapi.co/api/v2/pokemon/${i}`;
promises.push(fetch(url).then((res) => res.json()));
}
Promise.all(promises).then((results) => {
const pokemon = results.map((result) => ({
name: result.name,
image: result.sprites['front_default'],
type: result.types.map((type) => type.type.name).join(', '),
id: result.id
}));
displayPokemon(pokemon);
});
};

const displayPokemon = (pokemon) => {
console.log(pokemon);
const pokemonHTMLString = pokemon
.map(
(pokeman) => `
<li class="card">
<img class="card-image" src="${pokeman.image}"/>
<h2 class="card-title">${pokeman.id}. ${pokeman.name}</h2>
<p class="card-subtitle">Type: ${pokeman.type}</p>
</li>
`
)
.join('');
pokedex.innerHTML = pokemonHTMLString;
};
fetchPokemon();
//Sorting//

// const grass = pokemon.filter(pokemon => pokemon.type === 'grass')
// if(pokemon.type === 'grass'){
// return true;
// }
// console.table(grass)

不确定你是否需要它,但这是我的 HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Quick's Pokedex</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="/css/animate.css" />
<link href="https://fonts.googleapis.com/css?family=Rubik&display=swap" rel="stylesheet"/>
</head>
<body>
<div class="header">
<img src="/assets/img/two.png" alt="Pokemon Logo" height="500" class="animated fadeInLeftBig logo" id="logo">
</div>
<div class="container">

<audio class="audio-player" loop src="/assets/audio/pokemon.mp3" controls>
Error: your web browser does not support this audio player.
</audio>
<h1 class="quicks-h1">Quick's Pokedex</h1>
<ul id="pokedex"></ul>
</div>
<script src="app.js"></script>
</body>
</html>

最佳答案

使用 sort() 函数。按类型排序使用 .sort((a, b) => a.type > b.type ? 1 : -1)

例子:

  const fetchPokemon = () => {
const promises = [];
for (let i = 1; i <= 150; i++) {
const url = `https://pokeapi.co/api/v2/pokemon/${i}`;
promises.push(fetch(url).then((res) => res.json()));
}
Promise.all(promises).then((results) => {
const pokemon = results.map((result) => ({
name: result.name,
image: result.sprites['front_default'],
type: result.types.map((type) => type.type.name).join(', '),
id: result.id
})).sort((a, b) => a.type > b.type ? 1 : -1);
displayPokemon(pokemon);
});
};
const displayPokemon = (pokemon) => {
const pokemonHTMLString = pokemon
.map(
(pokeman) => `
<li class="card">
<img class="card-image" src="${pokeman.image}"/>
<h2 class="card-title">${pokeman.id}. ${pokeman.name}</h2>
<p class="card-subtitle">Type: ${pokeman.type}</p>
</li>
`
)
.join('');
pokedex.innerHTML = pokemonHTMLString;
};
fetchPokemon();
<div class="container">
<h1 class="quicks-h1">Quick's Pokedex</h1>
<ul id="pokedex"></ul>
</div>

关于javascript - 使用带有用户输入的 vanilla js 从 (poke) API 中对 API 数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60163400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com