1

I have this array which is retrieved by a json_encode().When I execute

$.getJSON('beta.php' , function(data){
    console.log(data);
});

I get the result as follows

[
Object { StuId="1", fName="Saman", more...},
Object { StuId="2", fName="Marry", more...},
Object { StuId="3", fName="Navjoth", more...},
Object { StuId="4", fName="Jassu", more...}
]

I'm trying to iterate through this result using

$.each(data, function(key, value){

            for(var key in value){

                if(value.hasOwnProperty(key)){                  

                        $("#article tbody").html(                       
                        "<tr><td>" + value.StuId + 
                        "</td><td>" + value.fName + 
                        "</td><td>" + value.lName +
                        "</td><td>" + value.age +
                        "</td><td>" + value.grade + 
                        "</td></tr>");

                        $("article tbody").appendTo("document.body");               

                }

                }

        });

.I guess It is impossible because of the above format of the array.

If someone could explain why this is happening and how to correct it I would be really grateful.I want to know how to convert the above into the following format.

[
{"StuId":"1","fName":"Saman","lName":"Kumara","age":"14","grade":"A"},
{"StuId":"2","fName":"Marry","lName":"Vass","age":"12","grade":"B"},
{"StuId":"3","fName":"Navjoth","lName":"Bogal","age":"32","grade":"A"},
{"StuId":"4","fName":"Jassu","lName":"Singh","age":"22","grade":"E"}
]
5
  • you already asked similar questions here :stackoverflow.com/questions/6186339/… Commented Jun 2, 2011 at 6:15
  • you have already getting the desired output u want where the hell is problem?? [] := Array {} :=Object Commented Jun 2, 2011 at 6:28
  • I'm extremely sorry.but still I couldn't get what I want.that's why I asked it again.BTW I'm new to this whole jQuery subject. Commented Jun 2, 2011 at 6:43
  • then its better to learn by yourself; ( yahan tumhare q'n ka solution to mil jaega but tum kabhi seekh nahin paogi, sachhi! ) Commented Jun 2, 2011 at 6:49
  • 1
    @diEcho, of course I will get my answer here and also I will learn somehow.sachhii Commented Jun 2, 2011 at 6:54

2 Answers 2

2

You are overwriting the tables html with different values each time.

$(document).ready(function() {
  var html = "";
  var data = [
          { StuId:"1", fName:"Saman"},
          { StuId:"2", fName:"Marry"},
          { StuId:"3", fName:"Navjoth"},
          { StuId:"4", fName:"Jassu"}
          ]
  $.each(data, function(key, value){
    console.log(value + "--" + key);
    html += "<div><span>" + value.StuId + "</span><span>" + value.fName + "</span></div>";
  });
  $('body').html(html)
});

Loops in JavaScript are different from other languages. You need closures.

Have a look here:

http://www.mennovanslooten.nl/blog/post/62

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Christian,thanks alot :) It was a problem in the loop closure.Now I feel relieved thanks again :)
0

The base object is a javascript array, so can you not:

$.each(data, function(item) {
    var html = "<tr><td>" + item.StuId + "</td><td>" + item.fname + "</td></tr>";
    // etc.
});

?

1 Comment

I have tried this already. but this output a table with only one row and it prints "undefined" for all the values.This is why I used for in loop after reading an post in this forum. It doesn't even print all the rows in the table.also I can't figure out why the looping doesn't happen properly :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.