![](/img/trans.png)
[英]Creating an object from looping through an array of objects with a nested array. Javascript
[英]Angular JS: Pushing objects into an array. It is creating an object, then another array. I need it to create separate objects. Is this possible?
在将“ multiple = true”添加到我的选择中之后,它将在模型中添加一个对象数组,而不是分别添加每个对象。
这就是我要的:
{
teams: [
{name: 'team1'},
{name: 'team2'},
{name: 'team3'}
]
}
但是相反,我越来越
{
teams: [
{name: 'team1'},
{name: ['team2','team3']}
]
}
数据结构问题的图片。
2手动放置的团队是对象。 使用md-select放置的团队是对象内的数组。
$scope.baseGitblituser = {
CODE HERE DELETED FOR READABILITY, WAS FOR OTHER USER FIELDS
teams:
[{name: "xignorex All Users"},
{name: "Test Users"}]
};
这将为用户启动作用域,并手动添加所有用户将包含的全局团队。
// this is created elsewhere using $scope.gitblitUser = angular.copy($scope.baseGitblituser);
$scope.gitblitUser.teams.push({name:user.team});
这是单击md-select按钮创建新用户时调用的函数的一部分。 这就是使用新团队更新团队数组的方式。
<md-select ng-model="user.team" multiple="true" name="teams" placeholder="your default team " style="min-width: 200px;">
<md-optgroup label="">
<md-option ng-value="team.name" ng-repeat="team in teams">{{team.name}}</md-option>
</md-optgroup>
这是HTML部分。
因此,凯文B推荐给我的链接可能有用...但是,这对我来说有点有趣,并且大大改变了我的html。 但! 我在其中看到了for循环...尽管使用方式有所不同,但它给了我一个想法,尝试使for循环混乱以将对象分别从数组中拉出。
$scope.gitblitUser.teams.push({name:user.team});
变成 -
for (var i=0; i < user.team.length; i++){ $scope.gitblitUser.teams.push({name: user.team[i]}); }
因此,最终...您的链接帮助激发了for循环的想法-非常感谢!
以为我会在这里发布这个信息,以防万一有人遇到我将来遇到的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.