【发布时间】:2015-10-19 10:11:32
【问题描述】:
首先我很抱歉如果它是重复的(我搜索但没有找到这个简单的例子......),但我想根据arr2 中的索引选择arr1 的元素:
arr1 = [33,66,77,8,99]
arr2 = [2,0,3]
我正在使用underscore.js但是0索引没有被检索到(似乎被认为是false):
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return index;
}
});
返回:
# [77, 8]
我该如何解决这个问题,是否有更简单的方法来使用索引数组进行过滤?我期待以下结果:
# [77, 33, 8]
【问题讨论】:
-
将 if 语句更改为 if(_.contains(arr2, index) >= 0)
-
@82Tuskers 我认为您将
_.contains与indexOf混淆了。 -
绝对正确! @joews 感谢您指出这一点...
标签: javascript arrays underscore.js