Solution 1 :

As the results value is an array and the objects within that are comma-delimited, you could do:

let resultReturn = {
  success: true, 
  results: [
      "heading1, head2, head3",
      "string, 200, 1.1",
      "string2, 100, 1.2",
      ]
    }

function loaddata() {
  let r = resultReturn.results;
  let tbody = document.getElementById("datatable");

  for (let i = 0; i < 3; i++) {
    let thisRow = r[i];
    let newRow = "<tr>";
    let thisRowItems = thisRow.split(",");
    for (let j = 0; j < thisRowItems.length; j++) {
      newRow += "<td>" + thisRowItems[j] + "</td>";
    }
    newRow += "</tr>";
    tbody.innerHTML += newRow;
  }
}
window.onload = loaddata;

Note that I’ve wrapped this in a function for testing purposes, so you won’t need to include the window.onload line

Problem :

So I’ve previously made tables with XML formatted results, and JSON results that are "key: data" formatted, and I would access the data like: results.heading1, and use a map to put the data into a table, matching the key with the data.

A new client can only send their data in the below format, and I’m not sure how to add this into a table on my site when the keys are all in the first row, and not alongside each entry. I’ve done a lot of googling but nothing seems to be matching my circumstances!

How would I go about making a table when the keys(headings) are not alongside the data, but in the first result row instead?

The JSON data in question is shown as below. I make an API call to the client, and the response is in the same format as the example below:

resultReturn: {
  success: true, 
  results: [
      "ID, Name, Age",
      "A1, Bob, 31",
      "B1, John, 24",
      "C2, Doe, 160",
      ,
  resultsHTML: ""
    }

Comments

Comment posted by Uciebila

Can the close voter explain what they think needs clarity?

Comment posted by Jay

I am not the flagger for close but i can see the problem with the question. usually, it is best if you can put the full code that you are using to render the json and also an example json body data as well. I think that may have elicited a close vote on the question. right now, its almost unanswerable. at least for me 🙂

Comment posted by Uciebila

Ah thank you, will update to explain that the code shown is the return from an API call to the client, that is the full return.

By