0

I'm trying my best to loop through JSON data that I get from API and display it in the html table. I parsed the original JSON and displayed data accesing it's elements like this:

JSON(the data is from api):

{"PrekesID":656851,"Kodas":"6DUSA000000122","SandKodas1":"20402","Likutis_PP":0,"Likutis":11,"KAPKodas":"5902115799686","KAPPavad":"PRAUSTUVO VOŽTUVAS S","Kaina":40},{"PrekesID":656851,"Kodas":"6DUSA000000122","SandKodas1":"20406","Likutis_PP":11,"Likutis":11,"KAPKodas":"5902115799686","KAPPavad":"PRAUSTUVO VOŽTUVAS S","Kaina":40}

The whole json is decalred as "content" so:

 let jsn=JSON.parse(content);

     document.getElementById("Prekes_Kodas").innerText="Kodas: "+jsn.Kodas;
                                
     document.getElementById("Prekes_Barkodas").innerText="Barkodas: "+jsn.KAPKodas;

And till this point everything wokrs fine. But I'm facing a problem that if JSON type is object and I loop through it and access it's elements or even get the length. Any suggestions?

Function(data is the parsed JSON):

    function warehouseQuant(data){
                let table = document.getElementById("myTable");
    
                for (let i = 0; i < data.length; i++ ){
                    let row = `<tr>
                                    <td>${data[i].Kodas}</td>
<td>${data[i].Kaina}</td>
<td>${data[i].Likutis}</td>
                                </tr>`
                                table.innerHTML += row 
                }
                }
1
  • 2
    If I understand correctly, jsn/data sometimes is an array, and sometimes an object? If so, a quick fix could be to put if(!data.length)data=[data] at the beginning of your procedure Commented Aug 20, 2021 at 7:18

1 Answer 1

4

Assemble all the rows and insert it as one monolithic chunk of html.

Your data doesn't have the outer brackets, but I'm assuming it's an array of objects. (If it's originally provided as you present it, it's not valid JSON.)

const data = [{"PrekesID":656851,"Kodas":"6DUSA000000122","SandKodas1":"20402","Likutis_PP":0,"Likutis":11,"KAPKodas":"5902115799686","KAPPavad":"PRAUSTUVO VOŽTUVAS S","Kaina":40},{"PrekesID":656851,"Kodas":"6DUSA000000122","SandKodas1":"20406","Likutis_PP":11,"Likutis":11,"KAPKodas":"5902115799686","KAPPavad":"PRAUSTUVO VOŽTUVAS S","Kaina":40}];

const warehouseQuant = data =>
  document.getElementById("myTable").innerHTML = data.map(
    item => ([
      '<tr>',
      ['Kodas','Kaina','Likutis'].map(
        key => `<td>${item[key]}</td>`
      ),
      '</tr>'
    ])
  ).flat(Infinity).join('');
  
warehouseQuant(data);
<table id="myTable"></table>

Sign up to request clarification or add additional context in comments.

3 Comments

Hey do you mind explaining (['<tr>', ['Kodas','Kaina','Likutis'].map(key => <td>${item[key]}</td>),'</tr>']) ?
@KarimAli Comments aren't a good place to answer questions. You might be better off asking a new question. But, what about it would you like explained?
@Ourborus, just needed to understand the statement (['<tr>', ['Kodas','Kaina','Likutis'].map(key => <td>${item[key]}</td>),'</tr>']) in the map function. I think I got it. It is clear, I misunderstood with a comma operator in javascript. Txs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.