[Solved] Javascript JSON array of objects to HTML table


The order is wrong. out is an object of arrays (not an array of objects). Here I fixed the order of access with the assumption, that all properties contain an array with the same length:

function fetch() {
  return Promise.resolve({
    json() {
      return Promise.resolve({
        "name": [
          "John",
          "Marie",
          "Clara"
        ],
        "surname": [
          "Doe",
          "Jane",
          "Smith"
        ],
        "phone": [
          "1233215555",
          "1233215555",
          "1233215555"
        ],
        "birthdate": [
          "1980-12-14",
          "1990-02-13",
          "1995-03-10"
        ]
      })
    }
  });
}

var url = "some-url";

fetch(url)
  .then(res => res.json())
  .then((out) => {
    for (let i = 0; i < out.name.length; i++) {
      console.log(out.name[i] + " " + out.surname[i] + " " + out.phone[i] + " " + out.birthdate[i])
    }
  })

4

solved Javascript JSON array of objects to HTML table