【问题标题】:angularjs foreach loop issueangularjs foreach循环问题
【发布时间】:2016-03-03 13:18:28
【问题描述】:

我是 AngularJS 的新手,我想用来自 json 文件的数据构建一个 object

这是我的json 文件:

[
  {"id": 1, "name": "user1", "select": false },
  {"id": 2, "name": "user2", "select": false },
  {"id": 3, "name": "user3", "select": false },
  {"id": 4, "name": "user4", "select": false },
  {"id": 5, "name": "user5", "select": false }
]

现在我想使用foreach 循环来检查哪个用户获得了select == true 并将这个用户名推送到新的array。所以这是我的第一次尝试:

'use strict';

angular.module('apiOmat', [])


  .controller('UsersCtrl', function($scope, $http){
    $http.get('users.json').then(function(usersResponse) {
      $scope.users = usersResponse.data;
    });


$scope.submit = function(message,title){

var tempArr = [];
angular.forEach($scope.users.name, function(value,key){
 tempArr.push(value);
});

console.log(tempArr);

    $scope.messagebody = '{ "title" = "' + title + '", "message" = "' + message  + '"}'; 
}

 });

我也试过这个:

  $scope.submit = function(message,title){

    var tempArr = [];
    angular.forEach($scope.users, function(value,key){
     tempArr.push( { key :  value } );
    });

    console.log(tempArr);

控制台记录了 5 个对象,但没有任何值。只是 1:对象 2:对象 3:对象 ...

我知道对 true 或 false 的查询丢失了。但我想在添加查询之前修复此步骤。

【问题讨论】:

  • 试试这个angular.forEach($scope.users,function(user){tempArr.push( user)})
  • 谢谢谢谢谢谢!它几乎起作用了。我只是使用 tempArr.push(username) 来获取我的用户名。
  • 这不是每个循环问题的角度

标签: javascript angularjs foreach


【解决方案1】:

为此:

现在我想使用一个 foreach 循环来检查哪个用户得到了 选择 == true 并将这个用户名推送到我的新数组中

$scope.submit = function(message,title){
   var tempArr = [];
   angular.forEach($scope.users, function(item){
     if( item.select == true){
       tempArr.push(item);
     }
   });

另一个简单且更好的解决方案是改用filter

$scope.submit = function(message,title){
    var tempArr = ($scope.users).filter(function(d){
         return (d.select == true);
    });
 });

console.log(tempArr)

【讨论】:

  • 非常非常感谢:)
  • 试试第二个更好:)
【解决方案2】:

试试这个

   $scope.submit = function(message,title){
   var tempArr = [];
   angular.forEach($scope.users, function(user){
   tempArr.push( {"name":user.name} );
   });

【讨论】:

    【解决方案3】:

    试试这个

    var tempArr = [];
    angular.forEach($scope.users, function(user){
        user.select && tempArr.push(user.name);
    });
    
    console.dir(tempArr);
    

    【讨论】:

    • 可能是因为你所有的用户都有“select”=false??它工作正常。jsfiddle.net/qp9qapgm
    • 我没有复制所有内容。我的错,对不起:D
    • 你能给我解释一下吗?我没有得到那个“user.select && tempArr.push(user.name);”这是“if(item.select == true){ tempArr.push(item);”的缩写形式吗?
    • @Paili 是的。 Javascript 使用逻辑表达式的惰性求值。就我而言,它首先检查 user.select 是否为 true(或另一个转换为 true 的值,请参阅 js doc)。如果是,则 js 转到第二个表达式并对其进行评估。否则将跳过第二个表达式,因为所有结果都将等于 true,并且没有必要在 'false' 之后解析所有其他内容。
    • 非常感谢 :) @Boris Parnikel
    【解决方案4】:

    下面试试

    $scope.submit = function(message,title){
    
    var tempArr = [];
    angular.forEach($scope.users, function(i,j){
    
    if(i.select == true)
    {
     tempArr.push( i.name );
      }
    });
    
    console.log(tempArr);
    

    【讨论】:

    • 这个解决方案没有检查 select == true,我猜这是你在数组上迭代的主要目的。
    【解决方案5】:

    保持简单:

    $scope.submit = function(message, title){
       var tempArr = [];
       angular.forEach($scope.users, function(user){
        if( user.select == true) //filter select:true only
          tempArr.push( {"name_as_key":user.name} ); //name_as_key can be replaced if intended to
       });
    

    【讨论】:

      猜你喜欢
      • 2011-08-08
      • 2021-12-27
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多