Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


$scope.filterSecId = function(items) {
    var result = {};
    angular.forEach(items, function(value, key) {
        if (!value.hasOwnProperty('secId')) {
            result[key] = value;
        }
    });
    return result;
}
<div ng-repeat="(k,v) in filterSecId(items)">
    {{k}} {{v.pos}}
</div>

@Sebastian, sorry about that. But there are people who are egghead.io subscribers and I expect it's useful to them.

@kentcdodds don't post links which are not free!

Angular filters can only be applied to arrays, not objects.

Note: This is a perf anti-pattern now. Angular 1.3 has stateless filters now (egghead.io/lessons/) so you'd definitely want to create a filter for this.

You have two options here, 1) move $scope.items to an array or 2) pre-filter the ng-repeat items, like this:

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


<div ng-repeat="(k,v) in items | with:'secId'">
        {{k}} {{v.pos}}
 </div>
app.filter('with', function() {
  return function(items, field) {
        var result = {};
        angular.forEach(items, function(value, key) {
            if (!value.hasOwnProperty(field)) {
                result[key] = value;
            }
        });
        return result;
    };
});

And in html:

My solution would be create custom filter and use it:

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


ng-show="v.hasOwnProperty('secId')"

Thanks.. I used this but with ng-if instead

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


$scope.filterSecId = function(items) {
    var result = {};
    angular.forEach(items, function(value, key) {
        if (!value.hasOwnProperty('secId')) {
            result[key] = value;
        }
    });
    return result;
}
<div ng-repeat="(k,v) in filterSecId(items)">
    {{k}} {{v.pos}}
</div>

"Selects a subset of items from array and returns it as a new array."

@kentcdodds don't post links which are not free!

Angular filters can only be applied to arrays and not objects, from angular's API -

Note: This is a perf anti-pattern now. Angular 1.3 has stateless filters now (egghead.io/lessons/) so you'd definitely want to create a filter for this.

Why not just add an ng-if on the repeated element?

You have two options here: 1) move $scope.items to an array or - 2) pre-filter the ng-repeat items, like this:

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


<b>Example1:</b>
  <p ng-repeat="item in items | toArray: true | pick: 'secId'">
    {{ item.$key }}, {{ item }}
  </p>

  <b>Example2:</b>
  <p ng-repeat="item in nestedItems | toArray: true | pick: 'secId || details.secId' ">
    {{ item.$key }}, {{ item }}
  </p>
angular.module('app', ['angular.filter'])
  .controller('MainCtrl', function($scope) {
  //your example data
  $scope.items = { 
    'A2F0C7':{ secId:'12345', pos:'a20' },
    'C8B3D1':{ pos:'b10' }
  };
  //more advantage example
  $scope.nestedItems = { 
    'A2F0C7':{
      details: { secId:'12345', pos:'a20' }
    },
    'C8B3D1':{
      details: { pos:'a20' }
    },
    'F5B3R1': { secId:'12345', pos:'a20' }
  };
});

It looks like the toArray filter is not present anymore. Is there a replacement, because the filter filter still does not allow objects?

You can simply use angular.filter module, and then you can filter even by nested properties. see: jsbin2 Examples:

You should disclose that angular.filter is not a core angular module and you are the author of it.

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


<div ng-repeat="(key, value) in order" ng-if="value > 0">

Also you can use ng-repeat with ng-if:

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


<div ng-controller="TestCtrl">
 <div ng-repeat="(k,v) in items | filter:{secId: '!!'}">
   {{k}} {{v.pos}}
 </div>
</div>

How did you get this to work? When I use a filter with an ng-repeated object, I get an error which is what is expected base on the Angular documentation.

It is kind of late, but I looked for e similar filter and ended using something like this:

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


$scope.filterSecId = function(items) {
    var result = {};
    angular.forEach(items, function(value, key) {
        if (!value.hasOwnProperty('secId')) {
            result[key] = value;
        }
    });
    return result;
}
<div ng-repeat="(k,v) in filterSecId(items)">
    {{k}} {{v.pos}}
</div>

"Selects a subset of items from array and returns it as a new array."

@kentcdodds don't post links which are not free!

Angular filters can only be applied to arrays and not objects, from angular's API -

Note: This is a perf anti-pattern now. Angular 1.3 has stateless filters now (egghead.io/lessons/) so you'd definitely want to create a filter for this.

Why not just add an ng-if on the repeated element?

You have two options here: 1) move $scope.items to an array or - 2) pre-filter the ng-repeat items, like this:

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


<b>Example1:</b>
  <p ng-repeat="item in items | toArray: true | pick: 'secId'">
    {{ item.$key }}, {{ item }}
  </p>

  <b>Example2:</b>
  <p ng-repeat="item in nestedItems | toArray: true | pick: 'secId || details.secId' ">
    {{ item.$key }}, {{ item }}
  </p>
angular.module('app', ['angular.filter'])
  .controller('MainCtrl', function($scope) {
  //your example data
  $scope.items = { 
    'A2F0C7':{ secId:'12345', pos:'a20' },
    'C8B3D1':{ pos:'b10' }
  };
  //more advantage example
  $scope.nestedItems = { 
    'A2F0C7':{
      details: { secId:'12345', pos:'a20' }
    },
    'C8B3D1':{
      details: { pos:'a20' }
    },
    'F5B3R1': { secId:'12345', pos:'a20' }
  };
});

It looks like the toArray filter is not present anymore. Is there a replacement, because the filter filter still does not allow objects?

You can simply use angular.filter module, and then you can filter even by nested properties. see: jsbin2 Examples:

You should disclose that angular.filter is not a core angular module and you are the author of it.

Note
Rectangle 27 0

javascript How to filter (key, value) with ng repeat in AngularJs?


<div ng-repeat="(k,v) in items | with:'secId'">
        {{k}} {{v.pos}}
 </div>
app.filter('with', function() {
  return function(items, field) {
        var result = {};
        angular.forEach(items, function(value, key) {
            if (!value.hasOwnProperty(field)) {
                result[key] = value;
            }
        });
        return result;
    };
});

And in html:

My solution would be create custom filter and use it:

Note