查找小于或大于underscore.js

abh*_*493 1 underscore.js

我们正在使用AngularJS使用underscore.js,我们使用_.where查找数据

$scope.myData=[{age:15}]
Run Code Online (Sandbox Code Playgroud)

因此,要找到它,我们正在使用

_.where($scope.myData,{age:15})
Run Code Online (Sandbox Code Playgroud)

但是要找到类似_.where($ scope.myData,{age <15})的东西,它给出了错误,有什么办法可以做。或者我们必须使用_.filter

_.filter($scope.myData,function(val){
    if(val.age<20)
    {
     return val
    }})
Run Code Online (Sandbox Code Playgroud)

fir*_*oit 5

从Underscore源代码中可以看到wherefilter

_.where = function(obj, attrs) {
  return _.filter(obj, _.matches(attrs));
};
Run Code Online (Sandbox Code Playgroud)

http://underscorejs.org/docs/underscore.html#section-28

所以,是的,您应该使用filter

_.filter(myArray, function(v) { return v.age < 15 })
Run Code Online (Sandbox Code Playgroud)