如果条件将所有项添加到数组

时间:2017-04-13 08:26:48

标签: javascript html angularjs arrays

我的角度应用中有一个用户列表,如果用户if将其推入age > 45阵列,我想要做的就是过滤validForScheme条件的用户它推动了阵列中的所有用户。我无法弄清楚出了什么问题。

另一件事是ng-repeat显示一个额外的空白项目。

注意:我使用$scope.allUsers方法从数据库中获取http



var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {

$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];
$scope.validForScheme = [];

$scope.filterUsers = function(arg) {
 if (arg.age > 45)
   console.log(arg.name, 'is valid for Scheme');
   $scope.validForScheme.push(arg);
}

$scope.allUsers.forEach(function(item, index){
  $scope.filterUsers(item);
})
console.log($scope.validForScheme);

});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <ol>
    <li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}<li>
  </ol>
</div>
&#13;
&#13;
&#13;

7 个答案:

答案 0 :(得分:3)

直接使用Array.filter代替。像这样:

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {

$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];

$scope.validForScheme = $scope.allUsers.filter(function(item, index){
  return item.age > 45
})
console.log($scope.validForScheme);

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <ol>
    <li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}</li>
  </ol>
</div>

答案 1 :(得分:2)

您需要在if {}中附上多个语句。

&#13;
&#13;
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {

$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];
$scope.validForScheme = [];

$scope.filterUsers = function(arg) {
 if (arg.age > 45){
     //console.log(arg.name, 'is valid for Scheme');
     $scope.validForScheme.push(arg);
   }
}

$scope.allUsers.forEach(function(item, index){
  //console.log(item)
  $scope.filterUsers(item);
})
//console.log($scope.validForScheme);

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <ol>
    <li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}</li>
  </ol>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您需要将if语句的内容括在大括号{}中。

如果没有括在大括号中,if只考虑一个(立即)行/语句作为其范围。

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {

  $scope.allUsers = [{
    "age": 54,
    "name": "Corrine Benson"
  }, {
    "age": 38,
    "name": "Thompson Newton"
  }, {
    "age": 62,
    "name": "Molly Good"
  }, {
    "age": 66,
    "name": "Kristine Mcbride"
  }, {
    "age": 45,
    "name": "Kathy Estrada"
  }, {
    "age": 22,
    "name": "Krystal Davis"
  }, {
    "age": 53,
    "name": "Butler Greene"
  }, {
    "age": 34,
    "name": "Hogan Joseph"
  }];

  $scope.validForScheme = [];

  $scope.filterUsers = function(arg) {
    if (arg.age > 45) {
      console.log(arg.name, 'is valid for Scheme');
      $scope.validForScheme.push(arg);
    }
  }

  $scope.allUsers.forEach(function(item, index) {
    $scope.filterUsers(item);
  })
  console.log($scope.validForScheme);

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <ol>
    <li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}
    </li>
  </ol>
</div>

答案 3 :(得分:1)

如果陈述缺少括号

        $scope.filterUsers = function (arg) {
            if (arg.age > 45) {
                console.log(arg.name, 'is valid for Scheme');
                $scope.validForScheme.push(arg);
            }
        }

工作代码。

&#13;
&#13;
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>


<body>
    <div ng-app="myApp" ng-controller="myCtrl">
        <ol>
            <li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}
                <li>
        </ol>
    </div>

    <script type="text/javascript">
        var app = angular.module('myApp', []);

        app.controller('myCtrl', function ($scope) {

            $scope.allUsers = [{
                "age": 54,
                "name": "Corrine Benson"
            }, {
                "age": 38,
                "name": "Thompson Newton"
            }, {
                "age": 62,
                "name": "Molly Good"
            }, {
                "age": 66,
                "name": "Kristine Mcbride"
            }, {
                "age": 45,
                "name": "Kathy Estrada"
            }, {
                "age": 22,
                "name": "Krystal Davis"
            }, {
                "age": 53,
                "name": "Butler Greene"
            }, {
                "age": 34,
                "name": "Hogan Joseph"
            }];
            $scope.validForScheme = [];

            $scope.filterUsers = function (arg) {
                if (arg.age > 45) {
                    console.log(arg.name, 'is valid for Scheme');
                    $scope.validForScheme.push(arg);
                }
            }

            $scope.allUsers.forEach(function (item, index) {
                $scope.filterUsers(item);
            })
            console.log($scope.validForScheme);

        });
    </script>
</body>

</html>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

答案 5 :(得分:0)

为了更简单,更清晰,更可靠的实现,只使用数组过滤器:

$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];

$scope.validForScheme = $scope.allUsers.filter(user => user.age > 45);

答案 6 :(得分:0)

你错过了条件之后的括号,这就是为什么立即将console.log视为条件语句而不是你的$scope.validForScheme.push(arg);