角度过滤器字符串数组的对象值

时间:2016-09-03 20:26:16

标签: javascript angularjs json angularjs-filter

我有两个数组。 首先是$scope.termini,其中包含字符串,另一个是$scope.reserved,其中包含对象。每个对象都有变量vrijemeTermina。我想按$scope.termini过滤$scope.reserved[x].vrijemeTermina

reserve.controller('resCtrl', function($scope, $http) {
    $scope.$watch("pickedDate", function(){
      $scope.termini=["13:00","15:00","17:00","19:00","21:00"];
        $http.post('php/dropdown.php',{"datum":$scope.pickedDate}).success(function(data){
          $scope.reserved = data;
          //alert(JSON.stringify(data));
          //alert($scope.reserved[0].datumTermina);
          angular.forEach($scope.reserved, function(value, key){
          $scope.termini = $filter('filter')($scope.termini, value.vrijemeTermina, false);
        });
      });
      console.debug("%o", $scope.termini);
      });
  });

我尝试了很多不起作用的东西。大多数答案我通过它的属性找到了过滤器对象数组。我还没找到解决这个问题的方法。

<?php
require_once(dirname(dirname(__FILE__))."/php/db-conn.php");

$data = json_decode(file_get_contents("php://input"));

$query = "SELECT * from termin WHERE datumTermina='".$data->datum."'";
$result = mysqli_query($con, $query);
$arr = array();
if(mysqli_num_rows($result) != 0) {
  while($row = mysqli_fetch_assoc($result)) {
    $arr[] = $row;
  }
}

$con->close();

echo json_encode($arr);

?>

这就是我的php文件。它会返回看起来像{0:{"id":"1";"vrijemeTermina":"13:00";"datumTermina":"03/09/2016"}}的json。

3 个答案:

答案 0 :(得分:0)

如何使用filter()纯粹的Javascript解决方案?

$scope.termini.filter(function(element, index, array) {

    return element == $scope.reserved[index].vrijemeTermina;

});

这是你正在寻找的吗?它将返回一个数组,其中包含$scope.termini中的所有元素,其中$scope.reserved[i].vrijemeTermina中的相应值(按索引)匹配。如果这不是你想要完成的事情,你会更清楚一点吗?

答案 1 :(得分:0)

试试这个:

$scope.termini = $scope.reserved.filter(function(v){
   return $scope.termini.filter(function(e){
              return e == v.vrijemeTermina
            }).length > 0;
});

这将创建一个数组,其中包含来自$scope.reserved的所有对象,其vrijemeTermina值等于原始$scope.termini数组中的一个值。

哦,我再次读到你的问题,我认为你希望这种方式相反。

$scope.termini = $scope.termini.filter(function(v){
   return $scope.reserved.filter(function(e){ 
              return e.vrijemeTermina == v
            }).length > 0;
});

答案 2 :(得分:0)

您可以在for循环中访问每个对象的vrijemeTermina属性,如下所示:

reserve.controller('resCtrl', function($scope, $http, filterFilter) {

    $scope.$watch("pickedDate", function() {

        $scope.termini = ["13:00","15:00","17:00","19:00","21:00"];

        $http.post('php/dropdown.php',{"datum":$scope.pickedDate})
        .then(function(response) {

            // make sure your data is parsed
            $scope.reserved = angular.fromJson(response.data);

            // check your data is an array
            if (!angular.isArray($scope.reserved)) {
                console.error("Expected $scope.reserved to be of type Array but is of type " + typeof($scope.reserved));
                return;
            }

            // loop through the array of objects
            for (var i = 0; i < $scope.reserved.length; i++) {

                // each iteration filter $scope.termini by the current objects vrijemeTermina value
                $scope.termini = filterFilter($scope.termini, $scope.reserved[i].vrijemeTermina, false);

            }

            console.debug("%o", $scope.termini);

        }, function(error) {

            // good practice to handle errors

        });

    });

});
相关问题