0

I am receiving data from backend to show in a table, but there's something i don't how to manage objects inside json are from 0 to 4 but there are more data with different name just like img1 I don't have any problems displaying data from 0-4 but can not get data from version1-4 . Number of version is limited to number of objects in comming, I mean if i have 0-9 objects i will have version1.....version-10

 const responseText = await response.json();
    console.log(responseText);
    if (!responseText.Err) {
      for (let i in responseText) {
        if (responseText.hasOwnProperty(i)) {
          if (responseText[i].id) {
            const entrega = responseText[i].versionData;
            const comment = responseText[i].comentario;
            const commnetTruncated = comment.substring(0, 40);
            const date = responseText[i].fecha;
            console.log(responseText.version1);
            newTableBody += `<tr> <td>${entrega}</td><td id='v${entrega}'></td><td>${commnetTruncated}...</td><td>${date}</td> </tr>`;
          }
        }
        
      }
      tableBody.innerHTML = newTableBody;
    }

Tried using responseText.version${i+1} but its not allowed

Appreciate your help!

json from backend

  • If you're having a different data inside your json object like: { 0: { "foo": "bazz", "foo": "bar", } } you may want to use a nested for loop to run through whole object. i.e. for loop on index object and then another loop on each indexed object – Hassaan Ali May 09 '22 at 03:35
  • Use `responseText[\`version${i+1}\`]` – Nick May 09 '22 at 04:23

0 Answers0