Austin Hunter Austin Hunter - 2 months ago 7
Javascript Question

Getting data out of a HUGE Array

So first things first, Here is my code for an array (explanation afterwards):



var UserProfiles = [{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'test',
token: '',
platform: 'android',
password: 'incorrect',
companyProfileID: 1,
UserTopics: [{
topicID: 1,
topicName: 'Needs Maintenance',
alertLevel: 'Urgent',
TopicDepartments: [{
departmentID: 1,
departmentName: 'Shop',
required: true,
DepartmentUsers: [{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'test',
token: '',
platform: 'android',
companyProfileID: 1
}]
}]
}]
}];





So what is UserProfiles? It is an array that will hold many user profiles. Each profile will have multiple UserTopics, and each UserTopics will have multiple TopicDepartments. So an array with an array with an array.... Mind blowing.
How will it be populated? Well it will be populated using a ionic registration app that will post to a server and insert into this array. But that is not my issue.
What is my issue? So what I have in my app is the code of what I posted. I have a single profile, lets say UserProfiles[0]. I need to get the UserTopics topicName out, at every index. But I am not sure on how to get each one out and put it into a dropdown menu in my app.

I have a



<select ng-model="selectSubject" ng-change="changedValue(selectSubject)" data-ng-options="option.subject for option in subject">
<option value="" disabled>Select a Subject</option>
</select>





That gets the user profile from storage and needs to get all of the UserTopics.topicName's associated.

I know I did a horrible job at explaining my issue. But I could really use some help, work with me and I can help explain.

Answer

The html is:

<select id="topic_name"></select>

Now the JS is as follow:

for(var i=0;i<UserProfiles.length;i++)
{
   var profile=UserProfiles[i];         //getting a profile

   var topic=profile.UserTopics;        //getting UserTopics array

   for(var j=0;j<topic.length;j++)
   {
      var topicname=topic[j].topicName;    //getting each topic name

      var topicdept=topic[j].TopicDepartments   //getting array of topic departments

       //now if you want more depth in topicdept array go on in same way

       html=html+"<option>"+topicname+"</option>";    //generating the dropdown list for topicname
   } 
}

$("#topic_name").html(html);
Comments