Data processing

0

I'm getting in json a category list that has the id and name data.

I do the restful in pure javascript and I can already see the data in the browser console, I am using this code:

   if (xhr.status == 200) {

    window.sessionStorage.setItem('lista', xhr.responseText);

    var lista = window.sessionStorage.getItem('lista');

    lista = JSON.parse(lista);

    let x = document.querySelector('#body')

    JSON.map(item => {

      x.innerHTML += '<tr><td>'+ item.title +'</td></tr>'
    });

}

My problem is that I do not know how to enter the data I receive into the table to display it to users. Can anyone at least tell the logic or show me an example code if it is possible?

    
asked by anonymous 09.02.2018 / 19:38

2 answers

0

One way to do this is to use innerHTML (I can not say that's the best way).

But it would be so

let json = [
  {title: 'Jobs'},
  {title: 'Mark'},
  {title: 'Bill'}
]


let x = document.getElementById('body')


json.map(item => {
  x.innerHTML += '<tr><td>'+ item.title +'</td></tr>'
})
<table>
    <thead>
      <tr>
        <th>Nomes</th>
      </tr>
    </thead>
    
    <tbody id="body">
        
    </tbody>
  </table>
    
09.02.2018 / 19:46
0

Let's understand the problem. As documented in the documentation, JSON contains methods for parsing JSON which are these:

For this reason, attempting to execute the code JSON.map(item => ... will return this error in Console :

  

TypeError: JSON.map is not a function

Because map() is not part of JSON

    
09.02.2018 / 23:14