Wednesday, March 6, 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

    plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

    In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...