skip to Main Content

hello I want to iterate an array of objects and render them in a table.
I’m pretty new to this so im struggling to iterate over each inner object.
Any help appreciated.
Here is my array of objects:
I can get a length of the whole array but not the length of each object(final Product).
Would I iterate over the whole array and then another loop for each object inside? How do i get the length of this object inside the array and should i use a for loop or for each loop?
many thanks.

var output = [finalProduct { DeviceName="gkorosi-lt2",  Product="Photoshop CC",  Status="Active"},
          finalProduct { DeviceName="gkorosi-lt3",  Product="Illustrator CC",  Status="Active"}];

2

Answers


  1. Chosen as BEST ANSWER

    Thanks. I had to keep my original data structure and using these For loops, I managed to get it to work.

    for (var i = 0; i < output.length; i++){
      $(".trbody").append("<td>"+ output[i].DeviceName.toUpperCase() + "</td>");
        //etc
    }
    

  2. If you whant to use JQuery, you can use $.each function

    var output = [{
        finalProduct: {
          DeviceName: "gkorosi-lt2",
          Product: "Photoshop CC",
          Status: "Active"
        }
      },
      {
        finalProduct: {
          DeviceName: "gkorosi-lt3",
          Product: "Illustrator CC",
          Status: "Active"
        }
      }
    ];
    
    $.each(output, function(key, value) {
      $("body").append("<div>" + value.finalProduct.DeviceName + "</div>");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search