DOM content from lists: Difference between revisions
		
		
		
		Jump to navigation
		Jump to search
		
No edit summary  | 
				|||
| Line 86: | Line 86: | ||
<div class=qu>  | <div class=qu>  | ||
<code>r.albums</code> is a list, each entry includes a <code>title</code> and <code>year</code>  | <code>r.albums</code> is a list, each entry includes a <code>title</code> and <code>year</code>  | ||
*  | *We use <code>map</code> to create <code>lst</code>  | ||
*Each element of <code>lst</code> must be added to the document body.  | |||
*If you don't want to add each of the 13 items by hand you'll have to find out how to use [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax|spread syntax].  | |||
<pre class=usr>  | <pre class=usr>  | ||
fetch('/beatles.json')  | fetch('/beatles.json')  | ||
Revision as of 14:22, 27 August 2021
beatles.json
{"members":
 [
   {"firstName":"Paul","lastName":"McCartney"},
   {"firstName":"John","lastName":"Lennon"},
   {"firstName":"Ringo","lastName":"Starr"},
   {"firstName":"George","lastName":"Harrison"}
 ],
 "albums":
 [
   {"title":"Please Please Me", "year":1963},
   {"title":"With the Beatles","year":1963},
   {"title":"A Hard Day's Night","year":1964},
   {"title":"Beatles for Sale","year":1964},
   {"title":"Help!","year":1965},
   {"title":"Rubber Soul","year":1965},
   {"title":"Revolver","year":1966},
   {"title":"Sgt. Pepper's Lonely Hearts Club Band","year":1967},
   {"title":"Magical Mystery Tour","year":1967},
   {"title":"White Album","year":1968},
   {"title":"Yellow Submarine","year":1969},
   {"title":"Abbey Road","year":1969},
   {"title":"Let It Be","year":1970}]
}
Using a for loop
- When you have data in a list you can use a for loop to generate content
 
Input
xxxxxxxxxxfetch('/beatles.json')  .then((r)=>r.json())  .then((r)=>{    for(let b of r.members){      let div = document.createElement('div');      div.innerHTML = `${b.lastName}, ${b.firstName}`;      document.body.append(div);    }  });Output
fetch('/beatles.json')
  .then((r)=>r.json())
  .then((r)=>{
    for(let b of r.members){
      let div = document.createElement('div');
      div.innerHTML = `${b.lastName}, ${b.firstName}`;
      document.body.append(div);
    }
  });
fetch('/beatles.json')
  .then((r)=>r.json())
  .then((r)=>{
    for(let beatle of r.members){
      let div = document.createElement('div');
      div.innerHTML = `${beatle.firstName} ${beatle.lastName}`;
      document.body.append(div);
    }
  });
2) Using forEach
- An alternative is the forEach method
 
Input
xxxxxxxxxxfetch('/beatles.json')  .then((r)=>r.json())  .then((r)=>{    r.members.forEach(b => {      let div = document.createElement('div');      div.innerHTML = `${b.lastName}, ${b.firstName}`;      document.body.append(div);    })  });Output
fetch('/beatles.json')
  .then((r)=>r.json())
  .then((r)=>{
    r.members.forEach(b => {
      let div = document.createElement('div');
      div.innerHTML = `${b.lastName}, ${b.firstName}`;
      document.body.append(div);
    })
  });
fetch('/beatles.json')
  .then((r)=>r.json())
  .then((r)=>{
    r.members.forEach(b => {
      let div = document.createElement('div');
      div.innerHTML = `${b.firstName} ${b.lastName}`;
      document.body.append(div);
    })
  });
3) Using map
r.albums is a list, each entry includes a title and year
- We use 
mapto createlst - Each element of 
lstmust be added to the document body. - If you don't want to add each of the 13 items by hand you'll have to find out how to use syntax.
 
Input
xxxxxxxxxxfetch('/beatles.json')  .then((r)=>r.json())  .then((r)=>{    let lst  = r.albums.forEach(a => {      let div = document.createElement('div');      div.innerHTML = `${a.title}, ${a.year}`;      return div    });    document.body.append(lst[0],lst[1],lst[2]);  });Output
fetch('/beatles.json')
  .then((r)=>r.json())
  .then((r)=>{
    let lst  = r.albums.forEach(a => {
      let div = document.createElement('div');
      div.innerHTML = `${a.title}, ${a.year}`;
      return div
    });
    document.body.append(lst[0],lst[1],lst[2]);
  });
fetch('/beatles.json')
  .then((r)=>r.json())
  .then((r)=>{
    document.body.append(... r.albums.map(b => {
      let div = document.createElement('div');
      div.innerHTML = `${b.title}, ${b.year}`;
      return div
    }));
  });