Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 带有IndexOf的下划线拒绝函数从数组中删除所有对象_Javascript_Angularjs_Underscore.js - Fatal编程技术网

Javascript 带有IndexOf的下划线拒绝函数从数组中删除所有对象

Javascript 带有IndexOf的下划线拒绝函数从数组中删除所有对象,javascript,angularjs,underscore.js,Javascript,Angularjs,Underscore.js,我正在编写一个小的Angular应用程序,它利用下划线查看数组中的每个对象,如果该对象与关键字(用户输入)不匹配,则删除该对象 $scope.search=function(){ $scope.posts=\.reject($scope.posts,函数(p){ var i=0; if($scope.keywords.indexOf(p.author)

我正在编写一个小的Angular应用程序,它利用下划线查看数组中的每个对象,如果该对象与关键字(用户输入)不匹配,则删除该对象

$scope.search=function(){
$scope.posts=\.reject($scope.posts,函数(p){
var i=0;
if($scope.keywords.indexOf(p.author)<0){
i++;
}
if($scope.keywords.indexOf(p.id)<0){
i++;
}
如果(i>0){
返回true;
}
});
};
如您所见,我正在设置一个计数器,然后如果在索引中找到关键字,则将其添加到计数器中,最后检查计数器以返回true或false,从而从数组中删除对象
$scope.posts
是包含我的数据的对象数组,
$scope.keywords
是用户输入。我想从
$scope.posts.author
对象和
$scope.posts.id
对象中查找输入


如果我删除其中一条
If
语句,函数将按预期执行:与关键字不匹配的所有内容都将从数组中删除。但是,只要我向函数添加另一个
if
语句(如上面的示例所示),所有对象都将从数组中删除

在我看来,
filter
可能更适合这里:

$scope.posts = _.filter($scope.posts, function(p) {
  return $scope.keywords.indexOf(p.author) > -1 || $scope.keywords.indexOf(p.id) > -1;
});

工作示例:

使用u.where以相反的方式进行过滤或拒绝会更容易

var newArray = _.where($scope.posts, {keyword : $scope.keyword});
好了,一行

编辑:

如果你坚持这样做,这里有一个方法你可以清理一下

$scope.posts = _.reject($scope.posts, function(p) {
  var check = false;
  if ($scope.keywords.indexOf(p.author) < 0 ) {
    check = true;
  }
  if ($scope.keywords.indexOf(p.id) < 0 ) {
    check = true;
  }
  if(i > 0) {
    return check;
  }
});
};
$scope.posts=\拒绝($scope.posts,函数(p){
var检查=假;
if($scope.keywords.indexOf(p.author)<0){
检查=正确;
}
if($scope.keywords.indexOf(p.id)<0){
检查=正确;
}
如果(i>0){
退货检查;
}
});
};

无需使用那样的整数

因为您要拒绝行,所以需要确保所有条件都为真。您的代码只是检查其中一个是否为真

 $scope.search = function() {
    $scope.posts = _.reject($scope.posts, function(p) {
      return (
           ($scope.keywords.indexOf(p.author) < 0 ) &&
           ($scope.keywords.indexOf(p.id) < 0 ) 
      );
    });
 };
$scope.search=function(){
$scope.posts=\.reject($scope.posts,函数(p){
返回(
($scope.keywords.indexOf(p.author)<0)&&
($scope.keywords.indexOf(p.id)<0)
);
});
};

对于这种类型的东西,
过滤器
会更好地匹配吗?
关键字
是一个stringpost对象没有关键字属性可匹配。用需要的任何内容填充它,这只是一个示例。代码看起来更好,但我得到的结果与示例中相同。删除所有对象。如果我删除逻辑运算符并再次检查它是否按预期工作。嗨,Stephen。我对代码进行了一些更新,并提供了一个工作示例。希望这有帮助。我要做的一个更改是将结果分配给不同的变量(如
$scope.searchResults
),而不是覆盖
$scope.posts
数据源。
 $scope.search = function() {
    $scope.posts = _.reject($scope.posts, function(p) {
      return (
           ($scope.keywords.indexOf(p.author) < 0 ) &&
           ($scope.keywords.indexOf(p.id) < 0 ) 
      );
    });
 };