underscore.js 使用上下文的过滤函数

underscore.js filter function using context

我无法理解 _.filter() 函数在 undescorejs 中的工作原理。具体来说,我试图了解是否有办法 return 来自被过滤对象的值,而不是索引。

假设如下:

var A = [1,2,3,4,5,8,10,12,15,20,25,30];

现在有了标准实施:

_.filter(A, function(x) { return x % 5 == 0 ;})

这将 return [5,10,15,20,25,30]。我的问题出现在以下方面:

_.filter([0,1,2,3,4,5,6], function(x) { return this[x] % 5 == 0 ;}, A)

This returns [4,6] 是真实值的索引(可被 5 整除的索引)。但我想 return 原始数组中真实索引的值,即 [5,10]。

根据我对其他 underscore.js 函数的理解,例如 _.each() 和 _.map() 这就是我使用上下文调用函数的方式。

_.map([0,1,2,3,4,5,6], function(x) { return this[x] % 5 == 0 ; }, A)

return [false,false,false,false,true,false,true]。我知道 _.filter() 正在内部调用 _.each() 来处理数组。因此,我对 _.filter([0,1,2,3,4,5,6], function(x) { return this[x] % 5 == 0 ;}, A) 的调用不起作用是有道理的,因为 _.each() 调用未在其函数中接收到 this[x] 值。

我只是遗漏了什么,还是没有办法调用 _.filter() 来 return 值?

像这样吗?:

var indexes = [0,1,2,3,4,5,6];
_.filter(A, function(x, i) {
    return x % 5 == 0 && indexes.indexOf(i) > -1;
});