i know the next question must seem stupid but i'll do it anyway :)
I've got this structure:
{
"name": "Erjet Malaj",
"first_name": "Erjet",
"work": [
{
"employer": {
"id": "110108059015688",
"name": "Art of Living Foundation"
},
"position": {
"id": "137081429661437",
"name": "Volunteer"
},
"start_date": "0000-00",
"end_date": "0000-00"
},
{
I retrieve response.data[i].first_name like this, how am i supposed to retrieve work id?
4 Answers 4
work
is an array. You need to loop through it
var work = response.data[i].work,
worklen = work.length,
j, workdetails;
for(j = 0; j < worklen; j++){
//simplify for readability
workdetails = work[j];
//where prop is "employer" or "position"
workdetails[prop].id
workdetails[prop].name
}
2 Comments
did you try response.data[i].work.position.id
?
on second thought, work is an array of objects, so it's accessed like :
response.data[i].work[0].position.id
Comments
Assuming you mean employer id of a given work
This should get the employer id
response.data[i].work[j].employer.id
You could also do this (depending on preference)
response.data[i].work[j]['employer'].id
In this case the work position is j (used as an array indexer), replace j with 0 to tget the first, with 1 to get the second etc.
Comments
There is an id
for the employer and position of each record in the work
array. If you want to loop over a person's work entries and collect those IDs, use:
var person = response.data[i];
var work = person.work;
for(var j = 0; j < work.length; ++j) {
var work_entry = work[j];
console.log(work_entry.employer.id);
console.log(work_entry.position.id);
}
The entries in the work
array are accessed numerically from 0. The entries themselves are objects, and you can access their properties using obj_name.property_name
or obj_name["property_name"]
work
doesn't have an ID. Do you mean the employer ID or position ID?