0

I have an object like:

{"result":
[
  {"id_":"18","name":"papas","species":"Other","sex":"F"},

{"id_":"26","name":"as","species":"bird","sex":"M"}

]}

How can I access each item, and print it inside a div?

I am trying:

$.each( myObj , function(index,item){
   $("#wrapper_busqueda").append('<div><b>' + myObj.id_ + '</b>  '+ myObj.mame  + ' </div><hr />');
}

but get undefined...

1
  • 1
    You have a typo... myObj.mame Commented Mar 29, 2013 at 16:39

3 Answers 3

1

You can use a for loop..

var OBJ = {"result":[{"id_":"18","name":"papas","species":"Other","sex":"F"},
{"id_":"26","name":"as","species":"bird","sex":"M"}]};

var total = OBJ['result'], myString = '';
for(i=0;i<total.length;i++)
{
    myString += '<div><b>' + total[i].id_ + '</b>  '+ total[i].mame  + ' </div><hr />';
}
$("#wrapper_busqueda").append(myString);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use as follows

$.each( myObj , function(index,item){
   $("#wrapper_busqueda").append('<div><b>' + item.id_ + '</b>  '+ item.mame  + ' </div><hr />');
}

It seems that you have multi dymension

for(var list in response)
{
   for(var item in list)
   {
     alert(item);
   }
 }

Comments

0

apart from the typo, you should access your myObj like this:

myObj['result']

and use item of the .each to access current item values and not directly myObj i.e. do item.name and not myOBj.name

hence

$.each( myObj['result'] , function(index,item){
   $("#wrapper_busqueda")
   .append('<div><b>' + item.id_ + '</b>  '+ item.name  + ' </div><hr />');
 });

see this fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.