-1

I have JSON data like this:

[0:{name:"jason",height:"150cm"},
1:{name:"henry",height:"178cm"}]

I'm trying to do a for loop in my function, which is

        function DrawTable(output) {

            var general = output;
            var sb = new StringBuilder();
            for (var i = 0; i < *total row in the json*; i++)
             sb.append("<td>" + general[0][i]["name"] + "</td>");
             sb.append("<td>" + general[0][i]["height"] + "</td>");
            }

I don't know the way to do it..

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
wyncent
  • 15
  • 2
  • 1
    Parse the JSON and read the `length` property. Don't forget that JSON is a string format. The length of a string/JSON is the number of characters. – jabaa Dec 14 '21 at 09:35
  • 1
    It's simply `.length` of array: `output.length` – Justinas Dec 14 '21 at 09:35
  • 1
    There's no [JSON](https://www.json.org/json-en.html) in your question. `output` is an array of objects. – Andreas Dec 14 '21 at 09:37
  • 1
    [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Andreas Dec 14 '21 at 09:39
  • 1
    You should deserialize the string into some kind of object(which would be an array). and then check the length – Royi Namir Dec 14 '21 at 09:47

2 Answers2

0

First off: that data isn't JSON.

For the sake of argument, let's pretend it was formatted as such:

[{
    "name": "jason",
    "height": "150cm"
}, {
    "name": "henry",
    "height": "178cm"
}]

Which would be valid JSON.

You could then do something more like this:

If using jQuery:

function DrawTable(jsonString) {
    var stuff = JSON.parse(jsonString);
    var table = createElement('table')
                    .append(
                           createElement('thead')
                              .append(
                                  createElement('tr')
                                      .append(
                                          createElement('th').text('Name'),
                                          createElement('th').text('Height')
                                      )
                              )
                    );
    var body = createElement('tbody');
    stuff.forEach(function(item) {
                      body
                         .append(
                             createElement('tr')
                                 .append(
                                      createElement('td').text(item.name),
                                      createElement('td').text(item.height)
                                 )
                         );
                  });
    //append body to table and show on page somewhere
}

Or, based on your existing code:

function DrawTable(output) {
    var general = JSON.parse(output);
    var sb = new StringBuilder();
    for (var i = 0; i < general.length; i++) {
        sb.append("<td>" + general[i].name + "</td>");
        sb.append("<td>" + general[i].height + "</td>");
    }
}
Andrew Corrigan
  • 1,017
  • 6
  • 23
0

If your data happens to be formatted like:

{
  0: {name:"jason",height:"150cm"},
  1: {name:"henry",height:"178cm"}
}

instead of wrapped in an array. Then looping through Objects.values(yourData) might be what you are looking for:

function DrawTable(objectsData) {
  var htmlString = '';
    
  Object.values(objectsData).forEach((object) => {
    htmlString += "<td>" + object.name + "</td>";
    htmlString += "<td>" + object.height + "</td>";
  });
    
  return htmlString;
}
digiwand
  • 1,258
  • 1
  • 12
  • 18