At first, I apologize if this is a duplicate (I searched, but did not find this simple example ...), but I want to select elements arr1
based on the index in arr2
:
arr1 = [33,66,77,8,99]
arr2 = [2,0,3]
I use underscore.js
, but the index is 0
not retrieved (it appears to be considered false
):
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return index;
}
});
What returns:
How can I fix this, and is there an easier way to filter using an array of indexes? I expect the following result:
source
share