[英]How to sort or filter JSON data by value in Javascript
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. 因此,我终于有了“星球大战” API,以显示“人”对象中每个字符的名称,该对象是从Vanilla Javascript中的https://swapi.co/api/people/获取的JSON数组。 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.
但是,我需要根据特定的值(即我拥有的当前代码https://swapi.co/api/species/1/)对数据结果进行排序或过滤,当我运行它时,它会显示带有以下名称的表所有物种的人,我只需要人类的物种。 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/ JSON端点来自https://swapi.co/api/people/
How do I get the table to only display the data for only the human species? 如何获得仅显示人类物种数据的表格?
那使用filter
呢?
const humansOnly = result.filter(p => p.species.indexOf('https://swapi.co/api/species/1/') !== -1);
Well, you know that https://swapi.co/api/species/1/
is human, so you can just check to see if that url exists in the species
array using filter
. 好吧,您知道
https://swapi.co/api/species/1/
是人类,因此您可以使用filter
来检查species
数组中是否存在该url。
const isHuman = guy=>~guy.species.indexOf('https://swapi.co/api/species/1/');
You can use indexOf
to determine an element exists in an array. 您可以使用
indexOf
确定数组中是否存在元素。 It returns negative one if it does not exist, so using bitwise NOT (~)
makes it truthy if it exist and falsey if it doesn't by turning a -1 into a zero. 如果不存在,它将返回负数1,因此通过将-1变为零,使用
bitwise NOT (~)
使其为真(如果存在),并为false(否)。
Then you can filter your results on that function before looping them.. 然后,您可以在循环搜索结果之前对该函数进行过滤。
data.results.filter(isHuman).forEach(result => {....});
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]; const isHuman = guy=>~guy.species.indexOf('https://swapi.co/api/species/1/'); fetchData('https://swapi.co/api/people/').then((data) => { data.results.filter(isHuman).forEach(result => { const row = constructTableRow(result); swTable.appendChild(row); }); });
<table id=sw-table><tbody></tbody></table>
The JSON returned by the species API includes a people
array. 物种API返回的JSON包含一个
people
数组。 So instead of filtering people
, loop over the people array. 因此,而不是过滤
people
,而是遍历人员数组。
However, this could cause a problem due to the SWAPI rate limiting . 但是,由于SWAPI 速率限制 ,这可能会引起问题。
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(url).then(data => data.people.forEach(personUrl => fetchData(personUrl).then(result => { const row = constructTableRow(result); swTable.appendChild(row); }) ) );
<table id=sw-table> <tbody></tbody> </table>
And another example using .filter()
and .includes()
(in place of detecting an negative index): 另一个使用
.filter()
和.includes()
示例(代替检测负索引):
fetchData('https://swapi.co/api/people/').then(data => {
data.results
.filter(person => person.species.includes("https://swapi.co/api/species/1/"))))
.forEach(human => swTable.appendChild(constructTableRow(human)))
});
does this work? 这有效吗?
fetchData('https://swapi.co/api/people/').then((data) => {
data.results.forEach(result => {
const row = constructTableRow(result);
if(result.species === "https://swapi.co/api/species/1/") {
swTable.appendChild(row);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.