angularjs $过滤器如何过滤2d数组

时间:2017-07-18 16:21:59

标签: angularjs filter

$ brew info python2

... snip ...

=> Caveats
This formula installs a python2 executable to /usr/local/bin.
If you wish to have this formula's python executable in your PATH then add
the following to ~/.bash_profile:
  export PATH="<... some path ...>:$PATH"

... snip ...

我想要结果[{“po”:1},{“po”:11},{“po”:12},{“po”:13},{“po”:21}] 当我调用$ scope.searchPo(1);

非常感谢

2 个答案:

答案 0 :(得分:0)

您也可以使用_.filter编写自己的函数。这是一种方法

  var searchPo = function (arr, id) {
   var newarr = []
   var reg = new RegExp(id)
   _.filter(arr, function(value) {
     _.filter(value, function(val) {
       _.each(val, function(v) {
              //console.log(reg.test(v));
             if(reg.test(v)) {
                   newarr.push(val);
             }
       });

     });  
  });

  console.log(newarr);
};

var a = [[{&#34; po&#34;:1},{&#34; po&#34;:2},{&#34; po&#34;:3}],[{ &#34; PO&#34;:11},{&#34; PO&#34;:12},{&#34; PO&#34;:13}],            [{&#34; PO&#34;:21},{&#34; PO&#34;:22},{&#34; PO&#34;:23}]];

searchPo(a,1)

答案 1 :(得分:0)

angular.module('app', [])
.controller('ctrl', ['$scope', '$filter', function($scope, $filter) {
    $scope.array = [
      [{"po":1},{"po":2},{"po":3}],
      [{"po":11},{"po":12},{"po":13}],
      [{"po":21},{"po":22},{"po":23}]
    ];

    $scope.searchPo = function(id){
      return $scope.array.reduce(function(a,b){
        return a.concat($filter('filter')(b, {po: id}));
      }, []);
    }        
   
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<ul ng-app='app' ng-controller='ctrl'>
   <li ng-repeat='item in searchPo(1)'>{{item}}</li>
</ul>