Reputation: 1220
I am looking to retrieve people_id
, username
, name
and people_type
from the following JSON data. Ultimately I have to populate these details in a bootstrap modal table. The following
is the jQuery code I have been using to retrieve the details :
.done(function (data_, textStatus_, jqXHR_) {
$.each(data_.people_list, function(key, value) {
$.each(value.details_list, function(k,v) {
console.log("Inside Loop:"+v.name);
$.each(v.role, function(k1,v1) {
console.log("Inside Another Loop for Role Name:"+v1.people_type);
});
});
});
})
At present I am trying to access only name
and people_type
.Here is the output I am getting on my console:
Inside Loop:First Hand Details
Inside Another Loop for Role Name:undefined
Inside Loop:First Hand Details
Inside Another Loop for Role Name:undefined
Inside Loop:UAB Multiple Myeloma Registry
Inside Another Loop for Role Name:undefined
Inside Loop:First Hand Details
Inside Another Loop for Role Name:undefined
Inside Loop:First Hand Details
Inside Another Loop for Role Name:undefined
Inside Loop:First Hand Details
Inside Another Loop for Role Name:undefined
Why people_type
is undefined? I read online that one gets undefined
when that particular thing doesn't exist?
{
"webservice_status" : {
"status" : "SUCCESS",
"message" : ""
},
"people_list" : [ {
"people_id" : 001,
"username" : "pxy123",
"full_name" : "James Camerion",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 002,
"username" : "gbt123",
"full_name" : "Tom Cruise",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3041,
"people_type" : "Normal User"
}
}, {
"id" : 5678,
"name" : "Second Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 003,
"username" : "txt456",
"full_name" : "Michael Jordon",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 004,
"username" : "mxn788",
"full_name" : "Paul Walker",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 005,
"username" : "nhgy234",
"full_name" : "Brad Pitt",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
} ]
}
Upvotes: 0
Views: 411
Reputation: 10975
To get expected result use below option
No need to loop v, as it is available with .each() and it is a single object and not having multiple objects
$.each(test[0].people_list, function(key, value) {
$.each(value.details_list, function(k,v) {
console.log("Inside Loop:"+v.name);
console.log("Inside Another Loop for Role Name:"+v.role.people_type);
});
});
var test =[{
"webservice_status" : {
"status" : "SUCCESS",
"message" : ""
},
"people_list" : [ {
"people_id" : 001,
"username" : "pxy123",
"full_name" : "James Camerion",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 002,
"username" : "gbt123",
"full_name" : "Tom Cruise",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3041,
"people_type" : "Normal User"
}
}, {
"id" : 5678,
"name" : "Second Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 003,
"username" : "txt456",
"full_name" : "Michael Jordon",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 004,
"username" : "mxn788",
"full_name" : "Paul Walker",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
}, {
"people_id" : 005,
"username" : "nhgy234",
"full_name" : "Brad Pitt",
"details_list" : [ {
"id" : 1234,
"name" : "First Hand Details",
"role" : {
"type_id" : 3042,
"people_type" : "SuperUser"
}
} ]
} ]
}];
$.each(test[0].people_list, function(key, value) {
$.each(value.details_list, function(k,v) {
console.log("Inside Loop:"+v.name);
console.log("Inside Another Loop for Role Name:"+v.role.people_type);
});
});
Codepen: http://codepen.io/nagasai/pen/BzrEVN
output:
Inside Loop:First Hand Details
Inside Another Loop for Role Name:SuperUser
Inside Loop:First Hand Details
Inside Another Loop for Role Name:Normal User
Inside Loop:Second Hand Details
Inside Another Loop for Role Name:SuperUser
Inside Loop:First Hand Details
Inside Another Loop for Role Name:SuperUser
Inside Loop:First Hand Details
Inside Another Loop for Role Name:SuperUser
Inside Loop:First Hand Details
Inside Another Loop for Role Name:SuperUser
Upvotes: 1