So I finally got star wars API to display the names of each character from the "people" object this JSON array taken from https://swapi.co/api/people/ in Vanilla Javascript. I need to, however, sort or filter the data results based on the specific value which is https://swapi.co/api/species/1/ the current code I have, when I run it displays the table with the names of all species of people, I only need the human species. Here is my code:
const url = 'https://swapi.co/api/species/1/?format=json';
function fetchData(url) {
return fetch(url).then((resp) => resp.json());
}
function constructTableRow(data) {
const row = document.createElement('tr');
const {
name,
height,
mass,
hair_color
} = data;
row.appendChild(constructElement('td', name))
row.appendChild(constructElement('td', height))
row.appendChild(constructElement('td', mass))
row.appendChild(constructElement('td', hair_color))
return row;
}
function constructElement(tagName, text, cssClasses) {
const el = document.createElement(tagName);
const content = document.createTextNode(text);
el.appendChild(content);
if (cssClasses) {
el.classList.add(...cssClasses);
}
return el;
}
const swTable = document.getElementById('sw-table').getElementsByTagName('tbody')[0];
fetchData('https://swapi.co/api/people/').then((data) => {
data.results.forEach(result => {
const row = constructTableRow(result);
swTable.appendChild(row);
});
});
<table id=sw-table><tbody></tbody></table>
The JSON endpoint come from https://swapi.co/api/people/
How do I get the table to only display the data for only the human species?