Data processing

Asked

Viewed 76 times

0

I am receiving in json a category list that has the id data and name.

I make restful in pure javascript and can already view 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 don’t know how to insert the data I receive into table to display it to users. Can someone at least say the logic or show me an example of code if possible?

2 answers

0


One way to do it is by using the innerHTML (I can’t say it’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>

  • I tried here @Rafaelaugusto, but it didn’t work. It says that JSON.map is not a function

  • I updated the code on my question. I put using your code

  • I put list.map instead of json,map and it put the tables on the screen, but it is written Undefined instead of the values. But it seems that the number of tables he entered is the same amount of values that exist in the list

  • It worked out, thanks

  • @Francisvagnerdaluz Show,

0

We will understand the problem. According to the documentation, JSON contains methods for Parsing JSON that are those:

For this reason when trying to execute the code JSON.map(item => ... will return this error on Console:

Typeerror: JSON.map is not a Function

For map() is not part of the object JSON

Browser other questions tagged

You are not signed in. Login or sign up in order to post.