0

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?

asked May 24, 2012 at 12:36
3
  • 2
    work doesn't have an ID. Do you mean the employer ID or position ID? Commented May 24, 2012 at 12:39
  • Where is work id in the structure? Commented May 24, 2012 at 12:39
  • work id means employer id OR position Id?.. JGD :) Commented May 24, 2012 at 12:41

4 Answers 4

2

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
}
Bergi
670k161 gold badges1k silver badges1.5k bronze badges
answered May 24, 2012 at 12:39

2 Comments

Hey, since i'm here, can i please make you a question, so i need to loop throuh all my id's then for each id extract the work id, what am i doing wrong here please? for(var i=0; i<response.data.length; i++){ 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 testdiv.innerHTML += workdetails; }
@AlAlb: Please edit your question and post the code there. Nothing is worse than multiline-code in comments...
0

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

answered May 24, 2012 at 12:38

Comments

0

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.

answered May 24, 2012 at 12:39

Comments

0

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"]

answered May 24, 2012 at 12:46

1 Comment

Thanks, this was all i needed, it took me time to understand how it works :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.