My JS code is this but I want to be able to get the moves
array to be displayed in HTML in a list format, how can I go about doing so?
JavaScript
x
13
13
1
const getData = () => {
2
axios
3
.get(" https://pokeapi.co/api/v2/pokemon/charmander")
4
.then((response) => {
5
const stats = response.data.moves;
6
const moves = stats.map((obj) => {
7
return obj.move.name;
8
});
9
})
10
.catch((error) => console.log(error));
11
};
12
13
Advertisement
Answer
You can use a safe helper to populate a node in the page, let’s say <div id="list"></div>
, so that your code can do something like:
JavaScript
1
16
16
1
import {render, html} from '//unpkg.com/uhtml?module';
2
3
const getData = () => {
4
axios
5
.get(" https://pokeapi.co/api/v2/pokemon/charmander")
6
.then((response) => {
7
const stats = response.data.moves;
8
render(document.getElementById('list'), html`
9
<ul>
10
${stats.map((obj) => html`<li>${obj.move.name}</li>`)}
11
</ul>
12
`);
13
})
14
.catch((error) => console.log(error));
15
};
16
That will also do the right thing next time you call getData
in case data changes.