简体   繁体   中英

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

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:

在此处输入图片说明

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));
     }

    });

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> 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM