How to Display data fetched from API call which is a two dimensional array












1















I am trying to call an API and display the data which is in the form of list, the problem I am facing is I am able to split the list and display it in HTML page, but I need to display appropriate label against the data. Any help in this regard is much appreciated. Thank you..



The data I got from API call is:




[[236,1,"1537890525704.jpg","","Residential","Rent",1200],[235,1,null,"","Residential","Rent",10000]]




The way I've displayed data on my HTML page is:



enter image description here



The way I want to display is appropriate label against the data, Ex:
Property Id : 236
No. of Bedrooms: 1
Property Type : Residential and so on..



client.html



<html>
<head>
<title>Test client application</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="cleint.js"></script>
<style>
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
width: 40%;
}

.card:hover {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}

.container {
padding: 2px 16px;
}
</style>

</head>

<body>


<p id="msgs" class="card container"></p>





client.js



$(document).ready(function() {   
$.ajax({
url: "http://192.168.1.5:8000/fetch",
success: function(data){
var c=data;
}
}).then(function(data) {
var text = "";
var res = "";
var i;
for(var i=0; i<data.length; i++){
var prop = data[i];
for(var j=0;j<prop.length;j++){
$('#msgs').append($('<div>').text(prop[j]));

}

}



});

function display(str) {
$('#msgs').append($('<div>').text(str));
}

});









share|improve this question



























    1















    I am trying to call an API and display the data which is in the form of list, the problem I am facing is I am able to split the list and display it in HTML page, but I need to display appropriate label against the data. Any help in this regard is much appreciated. Thank you..



    The data I got from API call is:




    [[236,1,"1537890525704.jpg","","Residential","Rent",1200],[235,1,null,"","Residential","Rent",10000]]




    The way I've displayed data on my HTML page is:



    enter image description here



    The way I want to display is appropriate label against the data, Ex:
    Property Id : 236
    No. of Bedrooms: 1
    Property Type : Residential and so on..



    client.html



    <html>
    <head>
    <title>Test client application</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="cleint.js"></script>
    <style>
    .card {
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
    transition: 0.3s;
    width: 40%;
    }

    .card:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
    }

    .container {
    padding: 2px 16px;
    }
    </style>

    </head>

    <body>


    <p id="msgs" class="card container"></p>





    client.js



    $(document).ready(function() {   
    $.ajax({
    url: "http://192.168.1.5:8000/fetch",
    success: function(data){
    var c=data;
    }
    }).then(function(data) {
    var text = "";
    var res = "";
    var i;
    for(var i=0; i<data.length; i++){
    var prop = data[i];
    for(var j=0;j<prop.length;j++){
    $('#msgs').append($('<div>').text(prop[j]));

    }

    }



    });

    function display(str) {
    $('#msgs').append($('<div>').text(str));
    }

    });









    share|improve this question

























      1












      1








      1








      I am trying to call an API and display the data which is in the form of list, the problem I am facing is I am able to split the list and display it in HTML page, but I need to display appropriate label against the data. Any help in this regard is much appreciated. Thank you..



      The data I got from API call is:




      [[236,1,"1537890525704.jpg","","Residential","Rent",1200],[235,1,null,"","Residential","Rent",10000]]




      The way I've displayed data on my HTML page is:



      enter image description here



      The way I want to display is appropriate label against the data, Ex:
      Property Id : 236
      No. of Bedrooms: 1
      Property Type : Residential and so on..



      client.html



      <html>
      <head>
      <title>Test client application</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
      <script src="cleint.js"></script>
      <style>
      .card {
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
      transition: 0.3s;
      width: 40%;
      }

      .card:hover {
      box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
      }

      .container {
      padding: 2px 16px;
      }
      </style>

      </head>

      <body>


      <p id="msgs" class="card container"></p>





      client.js



      $(document).ready(function() {   
      $.ajax({
      url: "http://192.168.1.5:8000/fetch",
      success: function(data){
      var c=data;
      }
      }).then(function(data) {
      var text = "";
      var res = "";
      var i;
      for(var i=0; i<data.length; i++){
      var prop = data[i];
      for(var j=0;j<prop.length;j++){
      $('#msgs').append($('<div>').text(prop[j]));

      }

      }



      });

      function display(str) {
      $('#msgs').append($('<div>').text(str));
      }

      });









      share|improve this question














      I am trying to call an API and display the data which is in the form of list, the problem I am facing is I am able to split the list and display it in HTML page, but I need to display appropriate label against the data. Any help in this regard is much appreciated. Thank you..



      The data I got from API call is:




      [[236,1,"1537890525704.jpg","","Residential","Rent",1200],[235,1,null,"","Residential","Rent",10000]]




      The way I've displayed data on my HTML page is:



      enter image description here



      The way I want to display is appropriate label against the data, Ex:
      Property Id : 236
      No. of Bedrooms: 1
      Property Type : Residential and so on..



      client.html



      <html>
      <head>
      <title>Test client application</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
      <script src="cleint.js"></script>
      <style>
      .card {
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
      transition: 0.3s;
      width: 40%;
      }

      .card:hover {
      box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
      }

      .container {
      padding: 2px 16px;
      }
      </style>

      </head>

      <body>


      <p id="msgs" class="card container"></p>





      client.js



      $(document).ready(function() {   
      $.ajax({
      url: "http://192.168.1.5:8000/fetch",
      success: function(data){
      var c=data;
      }
      }).then(function(data) {
      var text = "";
      var res = "";
      var i;
      for(var i=0; i<data.length; i++){
      var prop = data[i];
      for(var j=0;j<prop.length;j++){
      $('#msgs').append($('<div>').text(prop[j]));

      }

      }



      });

      function display(str) {
      $('#msgs').append($('<div>').text(str));
      }

      });






      javascript jquery html rest api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 9:24









      PraPra

      338




      338
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You need to build the HTML output within the loop. As each property is contained within an array you'll need to access it directly by index, and not through another inner loop. Try this:






          var data = [
          [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
          [235, 1, null, "", "Residential", "Rent", 10000]
          ]

          data.forEach(function(prop) {
          var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
          $('#msgs').append(`<div>${html}</div>`);
          });

          #msgs div {
          border: 1px solid #C00;
          padding: 10px;
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <div id="msgs"></div>








          share|improve this answer
























          • @ Rory McCrossan Thanks a ton. It worked like a charm.

            – Pra
            Nov 22 '18 at 9:38



















          0














          If you have fixed set of data in single array then you change it as..



          then(function(data) {
          var text = "";
          var res = "";
          var i;
          for(var i=0; i<data.length; i++){
          var prop = data[i];
          $('#msgs').append($('<div>').text("Property Id:"+ prop[0]));
          $('#msgs').append($('<div>').text("No. of Bedrooms:"+ prop[j]));
          });


          this ,might help you.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427597%2fhow-to-display-data-fetched-from-api-call-which-is-a-two-dimensional-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You need to build the HTML output within the loop. As each property is contained within an array you'll need to access it directly by index, and not through another inner loop. Try this:






            var data = [
            [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
            [235, 1, null, "", "Residential", "Rent", 10000]
            ]

            data.forEach(function(prop) {
            var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
            $('#msgs').append(`<div>${html}</div>`);
            });

            #msgs div {
            border: 1px solid #C00;
            padding: 10px;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div id="msgs"></div>








            share|improve this answer
























            • @ Rory McCrossan Thanks a ton. It worked like a charm.

              – Pra
              Nov 22 '18 at 9:38
















            2














            You need to build the HTML output within the loop. As each property is contained within an array you'll need to access it directly by index, and not through another inner loop. Try this:






            var data = [
            [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
            [235, 1, null, "", "Residential", "Rent", 10000]
            ]

            data.forEach(function(prop) {
            var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
            $('#msgs').append(`<div>${html}</div>`);
            });

            #msgs div {
            border: 1px solid #C00;
            padding: 10px;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div id="msgs"></div>








            share|improve this answer
























            • @ Rory McCrossan Thanks a ton. It worked like a charm.

              – Pra
              Nov 22 '18 at 9:38














            2












            2








            2







            You need to build the HTML output within the loop. As each property is contained within an array you'll need to access it directly by index, and not through another inner loop. Try this:






            var data = [
            [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
            [235, 1, null, "", "Residential", "Rent", 10000]
            ]

            data.forEach(function(prop) {
            var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
            $('#msgs').append(`<div>${html}</div>`);
            });

            #msgs div {
            border: 1px solid #C00;
            padding: 10px;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div id="msgs"></div>








            share|improve this answer













            You need to build the HTML output within the loop. As each property is contained within an array you'll need to access it directly by index, and not through another inner loop. Try this:






            var data = [
            [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
            [235, 1, null, "", "Residential", "Rent", 10000]
            ]

            data.forEach(function(prop) {
            var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
            $('#msgs').append(`<div>${html}</div>`);
            });

            #msgs div {
            border: 1px solid #C00;
            padding: 10px;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div id="msgs"></div>








            var data = [
            [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
            [235, 1, null, "", "Residential", "Rent", 10000]
            ]

            data.forEach(function(prop) {
            var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
            $('#msgs').append(`<div>${html}</div>`);
            });

            #msgs div {
            border: 1px solid #C00;
            padding: 10px;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div id="msgs"></div>





            var data = [
            [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
            [235, 1, null, "", "Residential", "Rent", 10000]
            ]

            data.forEach(function(prop) {
            var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
            $('#msgs').append(`<div>${html}</div>`);
            });

            #msgs div {
            border: 1px solid #C00;
            padding: 10px;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div id="msgs"></div>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 9:32









            Rory McCrossanRory McCrossan

            242k29206245




            242k29206245













            • @ Rory McCrossan Thanks a ton. It worked like a charm.

              – Pra
              Nov 22 '18 at 9:38



















            • @ Rory McCrossan Thanks a ton. It worked like a charm.

              – Pra
              Nov 22 '18 at 9:38

















            @ Rory McCrossan Thanks a ton. It worked like a charm.

            – Pra
            Nov 22 '18 at 9:38





            @ Rory McCrossan Thanks a ton. It worked like a charm.

            – Pra
            Nov 22 '18 at 9:38













            0














            If you have fixed set of data in single array then you change it as..



            then(function(data) {
            var text = "";
            var res = "";
            var i;
            for(var i=0; i<data.length; i++){
            var prop = data[i];
            $('#msgs').append($('<div>').text("Property Id:"+ prop[0]));
            $('#msgs').append($('<div>').text("No. of Bedrooms:"+ prop[j]));
            });


            this ,might help you.






            share|improve this answer




























              0














              If you have fixed set of data in single array then you change it as..



              then(function(data) {
              var text = "";
              var res = "";
              var i;
              for(var i=0; i<data.length; i++){
              var prop = data[i];
              $('#msgs').append($('<div>').text("Property Id:"+ prop[0]));
              $('#msgs').append($('<div>').text("No. of Bedrooms:"+ prop[j]));
              });


              this ,might help you.






              share|improve this answer


























                0












                0








                0







                If you have fixed set of data in single array then you change it as..



                then(function(data) {
                var text = "";
                var res = "";
                var i;
                for(var i=0; i<data.length; i++){
                var prop = data[i];
                $('#msgs').append($('<div>').text("Property Id:"+ prop[0]));
                $('#msgs').append($('<div>').text("No. of Bedrooms:"+ prop[j]));
                });


                this ,might help you.






                share|improve this answer













                If you have fixed set of data in single array then you change it as..



                then(function(data) {
                var text = "";
                var res = "";
                var i;
                for(var i=0; i<data.length; i++){
                var prop = data[i];
                $('#msgs').append($('<div>').text("Property Id:"+ prop[0]));
                $('#msgs').append($('<div>').text("No. of Bedrooms:"+ prop[j]));
                });


                this ,might help you.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 9:42









                Pratik RathiPratik Rathi

                66




                66






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427597%2fhow-to-display-data-fetched-from-api-call-which-is-a-two-dimensional-array%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python