0

I have a string which I get from an api call and then I parse it into an object using JSON.parse(meetResponse)

meetResponse = {
    "returncode":"SUCCESS",
    "meetingName":"bbb meeting",
    "meetingID":"712",
    "createTime":"1457969919738",
    "createDate":"Mon Mar 14 11:38:39 EDT 2016",
    "voiceBridge":"35014",
    "dialNumber":"613-555-1234",
    "attendeePW":"PDmAJD4n",
    "moderatorPW":"mpassword",
    "running":"true",
    "duration":"0",
    "hasUserJoined":"true",
    "recording":"true",
    "hasBeenForciblyEnded":"false",
    "startTime":"1457969919743",
    "endTime":"0","participantCount":"2",
    "maxUsers":"20",
    "moderatorCount":"2",
    "attendees":{
        "attendee":[
            {
                "userID":"10005655",
                "fullName":"Snedden Gonsalves",
                "role":"MODERATOR",
                "customdata":{}
            },{
                "userID":"10005656",
                "fullName":"SneddenReg Gonsalves",
                "role":"MODERATOR",
                "customdata":{}
            }
        ]
    },
    "metadata":{},
    "messageKey":{},
    "message":{}
}

I want to parse 'attendee' under 'attendees' to see who is present

The logic I use right now is :

      //check if current user is already present in the meeting
      for (var key in meetInfo.attendees.attendee){
         console.log('key:',meetInfo.attendees.attendee[key]);
         console.log(meetInfo.attendees.attendee[key].userID+"==="+user_id);
         if(meetInfo.attendees.attendee[key].userID===user_id){
             console.log('in meeting..');
             inMeeting=true;
             break;
             }
          else{
             inMeeting=false;
          }
  }

Note:meetInfo is the Whole object

This works is there are more than one attendee but for one attendee it fails. I am looking for something which would work for any number of 'attendees'.

Also I tried meetInfo.attendees.length instead of Object.keys(meetInfo.attendees).length but it didn't like it

1
  • It's probably more confusing to read XML then it would be if you properly formated your meetResponse. meetInfo.attendees.length would work if it was array but instead it's object with a property attendee (BTW it's confusing since you have attendee as an array) It should be more like meetInfo.attendees : [{ "userID":... }] Commented Mar 14, 2016 at 16:59

1 Answer 1

1

It sounds like your attendees.attendee property could be either an array if multiple, or an object if singular. When it is an array your key variable in the for..in block will be populated the index. When it is an object, it will be populated with the property key.

Two things. First, you can make sure you are always working with an array by concatenating the value with an empty array:

var attendeeList = [].concat(meetInfo.attendees.attendee); 

Second, you should not use for..in for iterate through an array. Use a classic for loop instead:

for (var idx= 0; idx < attendeeList.length; idx++)
    console.log('key:',attendeeList[idx]);
    console.log(attendeeList[idx].userID+"==="+user_id);
    if(attendeeList[idx].userID===user_id){
       console.log('in meeting..');
       inMeeting=true;
       break;
    } else{
       inMeeting=false;
    }
}

Bonus, this loop is setting a variable true if any of the items in the array match. There is a special Array function for this:

inMeeting = [].concat(meetInfo.attendees.attendee)
              .some(function(a){
                  return a.userID === user_id;
              });
Sign up to request clarification or add additional context in comments.

2 Comments

thanks this is exactly what I was looking for,also went thru the documentation of array.some() . Not sure if I should use it here as not sure if he breaks the loop when it finds the condition to be true,as I don't want it to loop when it doesn't need to
It does break the loop and is pretty easy to test: open a console and enter: [1,2,3,4,5,6].some(e => { console.log(e); return e == 2 })

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.