fetchBasic.js 545 B

1234567891011121314151617181920212223242526
  1. const renderTableFromJSON = (parent, jsonObj) => {
  2. const tableConstruct = json => {
  3. let tableElement = '<table>';
  4. for (const row in json) {
  5. tableElement += `
  6. <tr>
  7. <td>${row}</td>
  8. <td>${json[row]}</td>
  9. </tr>`;
  10. }
  11. tableElement += '</table>';
  12. return tableElement;
  13. }
  14. fetch(`${jsonObj}`)
  15. .then(response => response.json())
  16. .then(luke => {
  17. parent.insertAdjacentHTML('beforeend', tableConstruct(luke));
  18. });
  19. }
  20. renderTableFromJSON(document.body, 'https://swapi.dev/api/people/1/');