如何在Javascript中按值排序或过滤JSON数据

迈克L5799

因此,我终于有了Star Wars API,以显示来自JSON数组的“人”对象中每个人物的名称,该JSON数组来自Vanilla Javascript中的https://swapi.co/api/people/但是,我需要根据特定值对数据结果进行排序或过滤,该特定值是https://swapi.co/api/species/1/我当前拥有的代码,运行时它会显示带有以下名称的表所有物种的人,我只需要人类的物种。这是我的代码:

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>

JSON端点来自https://swapi.co/api/people/

如何获得仅显示人类物种数据的表格?

巴尔玛

物种API返回的JSON包含一个people数组。因此,而不是过滤people而是遍历people数组。

但是,由于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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在JavaScript中按日期范围过滤JSON数据

如何按值JavaScript排序JSON数据?

如何在javascript中按距离对Json中的数据进行排序?

如何在 JavaScript 中按日期范围从 API 过滤 JSON 数据

如何在JavaScript中按值过滤字典?

如何在json路径mysql中按数组值排序?

如何在javascript中按值对地图排序?

如何在JavaScript或jQuery中过滤JSON数据?

如何在Javascript中对Json数据进行排序?

如何在 Scala 中按值对 RDD 数据(键、值)进行排序?

如何在Excel中按数据对列进行排序/排序

如何在Pandas数据框中按行值对日期时间列进行排序?

如何在数据透视表中按混合值分组并按日期排序?

如何在ggplot方面内按值排序数据

如何在 Foreach PHP 中按数字值对 JSON 解码进行排序

如何在javascript中按属性值对多维对象数组进行排序?

如何在JavaScript中按值对整数键对象进行排序

如何在 JS 中过滤 JSON 数据

如何在 Django 中按 ArrayList 中的值排序?

如何在javascript中过滤数据集?

按日期过滤的 JavaScript 中嵌套 JSON 对象的总和值

如何在Java 8中按值过滤地图?

如何在ElasticSearch中按字段值过滤?

如何在 Pandas 中按条件值过滤行?

如何在JavaScript中过滤JSON对象?

如何在spark Scala中按值排序

如何在Julia中按值对字典排序?

如何在Activerecord中按计算值排序?

如何在PHP中按值对多维数组进行排序