简体   繁体   English

从庞大的阵列中获取数据

[英]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? 那么什么是UserProfiles? It is an array that will hold many user profiles. 它是一个包含许多用户配置文件的数组。 Each profile will have multiple UserTopics, and each UserTopics will have multiple TopicDepartments. 每个配置文件将具有多个UserTopics,并且每个UserTopics将具有多个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]. 我有一个个人资料,可以说UserProfiles [0]。 I need to get the UserTopics topicName out, at every index. 我需要在每个索引处获取UserTopics topicName。 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. 这样可以从存储中获取用户配置文件,并且需要获取所有与UserTopics.topicName相关的信息。

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. 但是我真的可以使用一些帮助,与我一起工作,并且我可以帮助解释。

The html is: 的HTML是:

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

Now the JS is as follow: 现在,JS如下所示:

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);

The method you are looking for is .map() if you are extracting data from objects in an array, or .forEach() if you are iterating over objects in an array and pushing them someplace else. 如果要从数组中的对象中提取数据, 则要使用的方法是.map();如果要遍历数组中的对象并将它们推到其他位置,则需要的方法是.forEach() Both take a function as an argument. 两者都以函数作为参数。 You can alias each profile and then extract whatever data you'd like from it. 您可以为每个配置文件添加别名,然后从中提取所需的任何数据。 Specifically: 特别:

var topics = [];

UserProfiles.forEach(function(profile){
    profile.UserTopics.forEach(function(topic) {
        topics.push(topic.topicName);
    });
});

Good luck! 祝好运!

Edit: Refactored so that all topics in the array will be added. 编辑:重构,以便将添加数组中的所有主题。

How about trying: 如何尝试:

<select ng-model="selectedTopic" 
        ng-options="userTopic as userTopic.topicName for userTopic in selectedUser.UserTopics track by userTopic.topicId">
</select>

This follows " select as label for value in array ". 这遵循“ 选择作为数组中 值的 标签 ”。

What it meant is that the ngModel will be assigned as the select expression, the select options will be displayed as the label expression. 这意味着ngModel将被分配为选择表达式,选择选项将显示为标签表达式。

Hope this snippet will be useful. 希望此摘要对您有所帮助。 Avoid using nested json object. 避免使用嵌套的json对象。 As it will require nested loops 因为它将需要嵌套循环

UserProfiles.forEach(function(item, index) {
  var getUserTopics = item.UserTopics;
  getUserTopics.forEach(function(userTopicItem, userTopicIndex){
    var getTopicName = userTopicItem.topicName;
    document.write('<pre>' + item.firstName + "--" + getTopicName + '</pre>')
  })
})

JSFIDDLE JSFIDDLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM