从巨大阵列中获取数据

时间:2016-09-10 04:23:44

标签: javascript html angularjs arrays

首先,首先,这是我的数组代码(后来的解释):

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
            }]
        }]
    }]
}];

那么UserProfiles是什么?它是一个包含许多用户配置文件的数组。每个配置文件都有多个UserTopics,每个UserTopics将有多个TopicDepartments。所以一个带有数组的数组的数组....令人兴奋。 它将如何填充?好吧,它将使用离子注册应用程序填充,该应用程序将发布到服务器并插入此数组。但这不是我的问题。 我的问题是什么?所以我在我的应用程序中的内容是我发布的代码。我有一个配置文件,比方说UserProfiles [0]。我需要在每个索引处获取UserTopics topicName。但我不确定如何将每个出来并将其放入我的应用程序的下拉菜单中。

我有一个

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

从存储中获取用户配置文件,需要将所有UserTopics.topicName关联起来。

我知道我在解释我的问题上做得很糟糕。但我真的可以使用一些帮助,和我一起工作,我可以帮忙解释一下。

4 个答案:

答案 0 :(得分:4)

html是:

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

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

答案 1 :(得分:1)

如果要从数组中的对象提取数据,则要查找的方法为.map(),如果要迭代数组中的对象并将其推送到其他位置,则.forEach()。两者都将函数作为参数。您可以为每个配置文件设置别名,然后从中提取您想要的任何数据。具体做法是:

var topics = [];

UserProfiles.forEach(function(profile){
    profile.UserTopics.forEach(function(topic) {
        topics.push(topic.topicName);
    });
});
祝你好运!

编辑:重构,以便添加数组中的所有主题。

答案 2 :(得分:0)

尝试怎么样:

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

数组&#34; <&gt; > / p>

这意味着ngModel将被指定为 select 表达式,选择选项将显示为标签表达式。

答案 3 :(得分:0)

希望此代码段有用。避免使用嵌套的json对象。因为它需要嵌套循环

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

相关问题