Wednesday 6 March 2019

jquery - How to display each element in Json object using $.each without providing every element separately



My Following code is working fine but I have to hard code each element separately.



How to display each element (myElement as shown below) in Json object using $.each without providing every element separately.



function getList() {
$.getJSON("MY_CONTROLLER_URL",function(data){

if(data) {
var json_data;
$.each(data, function(i,myObject){
debugger;
json_data = '
  • '+myObject.myElement+'
  • ';

    $(json_data).appendTo('#list-data');
    });
    }
    });


    Answer



    var x = {
    a: "Hello",
    b: "World"
    }


    Then you can use




    Object.keys(x) // => ["a", "b"]

    [].forEach.call(Object.keys(x), function(inst){
    console.log(x[inst]); // Will output a and then b
    });

    No comments:

    Post a Comment

    php - file_get_contents shows unexpected output while reading a file

    I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...