In Angular, I need to search objects in an array

后端 未结 7 1227
时光说笑
时光说笑 2020-12-04 07:30

In Angular, I have in scope a object which returns lots of objects. Each has an ID (this is stored in a flat file so no DB, and I seem to not be able to user ng-resour

7条回答
  •  时光取名叫无心
    2020-12-04 07:42

    To add to @migontech's answer and also his address his comment that you could "probably make it more generic", here's a way to do it. The below will allow you to search by any property:

    .filter('getByProperty', function() {
        return function(propertyName, propertyValue, collection) {
            var i=0, len=collection.length;
            for (; i

    The call to filter would then become:

    var found = $filter('getByProperty')('id', fish_id, $scope.fish);
    

    Note, I removed the unary(+) operator to allow for string-based matches...

提交回复
热议问题