Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Angular中,我需要搜索数组中的对象_Javascript_Angularjs - Fatal编程技术网

Javascript 在Angular中,我需要搜索数组中的对象

Javascript 在Angular中,我需要搜索数组中的对象,javascript,angularjs,Javascript,Angularjs,在Angular中,我有一个返回很多对象的对象。每个都有一个ID(它存储在一个平面文件中,因此没有DB,我似乎无法使用ng资源) 在我的控制器中: $scope.fish = [ {category:'freshwater', id:'1', name: 'trout', more:'false'}, {category:'freshwater', id:'2', name:'bass', more:'false'} ]; 在我看来,通过ng showmore默认情况下隐藏的鱼

在Angular中,我有一个返回很多对象的对象。每个都有一个ID(它存储在一个平面文件中,因此没有DB,我似乎无法使用
ng资源

在我的控制器中:

$scope.fish = [
    {category:'freshwater', id:'1', name: 'trout', more:'false'},
    {category:'freshwater', id:'2', name:'bass', more:'false'}
];
在我看来,通过
ng show
more默认情况下隐藏的鱼有其他信息,但是当我单击简单的show more选项卡时,我想调用函数
showdetails(fish.fish\u id)
。 我的函数类似于:

$scope.showdetails = function(fish_id) {  
    var fish = $scope.fish.get({id: fish_id});
    fish.more = true;
}
现在,在视图中会显示更多详细信息。然而,在搜索了文档之后,我不知道如何搜索
fish
数组


那么如何查询数组呢?在控制台中,如何调用调试器,以便使用
$scope
对象

我知道这是否能帮你一点忙

这是我试图为你模拟的东西

签出JSFIDLE;)

创建了一个也可在“ng repeat”中使用的筛选器

app.filter('getById', function() {
  return function(input, id) {
    var i=0, len=input.length;
    for (; i<len; i++) {
      if (+input[i].id == +id) {
        return input[i];
      }
    }
    return null;
  }
});

如果有任何问题,请告诉我。

一个肮脏而简单的解决方案可能是这样的

$scope.showdetails = function(fish_id) {
    angular.forEach($scope.fish, function(fish, key) {
        fish.more = fish.id == fish_id;
    });
};

您可以使用现有的$filter服务。我更新了上面的小提琴


Angular documentation在这里

是为了补充@migontech的答案,也是为了回应他的评论,即你可以“让它更通用”,这里有一个方法。以下内容将允许您按任何属性进行搜索:

.filter('getByProperty', function() {
    return function(propertyName, propertyValue, collection) {
        var i=0, len=collection.length;
        for (; i<len; i++) {
            if (collection[i][propertyName] == +propertyValue) {
                return collection[i];
            }
        }
        return null;
    }
});

注意,我删除了一元(+)运算符以允许基于字符串的匹配

Angularjs已经有了过滤选项,
您的解决方案是正确的,但并不复杂。您可以使用纯javascript过滤函数。这是你的模型:

     $scope.fishes = [{category:'freshwater', id:'1', name: 'trout', more:'false'},  {category:'freshwater', id:'2', name:'bass', more:'false'}];
这就是你的功能:

     $scope.showdetails = function(fish_id){
         var found = $scope.fishes.filter({id : fish_id});
         return found;
     };
您还可以使用表达式:

     $scope.showdetails = function(fish_id){
         var found = $scope.fishes.filter(function(fish){ return fish.id === fish_id });
         return found;
     };

有关此函数的详细信息:

看到此线程,但我想搜索与搜索不匹配的ID。执行此操作的代码:

     $scope.showdetails = function(fish_id){
         var found = $scope.fishes.filter({id : fish_id});
         return found;
     };
found = $filter('filter')($scope.fish, {id: '!fish_id'}, false);

考虑到文档说明这是ng init的唯一用例,我想说这绝对是一种角度上的方法。非常容易理解的示例,非常有用为什么这是一个肮脏的解决方案?我猜可能是因为,10亿条鱼的记录,我们只是一个接一个地循环浏览,还有更多其他语言的记录。我的意思是,C++中有大量的鱼。(哦,闭嘴..我会去投票否决自己..!!)非常有帮助-当我了解到这一点时,我意识到我需要先停止思考jQuery函数(我试图让$.grep来做这件事)-而使用$filter服务正是我所需要的!回答得更好,因为它不涉及编写您自己的筛选器。您可以添加
$scope.selected
是/包含的详细信息吗。对我找到的选定项进行快速搜索
如果表达式为truthy,则将在元素上设置特殊属性“selected”。这是一样的吗?在你的例子中,它做什么?谢谢!。让它变得简单。感谢不要忘记将$filter服务添加到控制器中。i、 e:app.controller('mainController',['$filter',函数($filter){/$filter现在可以使用。}]);由于我不熟悉angular和javascript,我不理解“+”在“if(+input[I].id==+id){”语句中的含义,请分享您的想法。完美的解决方案!!我认为“+input[I].id==+id”确保您正在比较数字。因此,您可以将1或“1”传递给$filter,它的行为将完全相同。我使用字母数字id,因此我将其更改为“input[I]。id==id”。这对我来说很有效。简单、流畅、易于测试。谢谢!
     $scope.showdetails = function(fish_id){
         var found = $scope.fishes.filter(function(fish){ return fish.id === fish_id });
         return found;
     };
found = $filter('filter')($scope.fish, {id: '!fish_id'}, false);