I want to filter the following objects by category_id using the following array of category_id values:
[19012000, 19012001, 19012002, 19012003, 19012004, 190
indexOf()
with a string and a number
var a = [123];
console.log(a.indexOf(123)); //true
console.log(a.indexOf("123")); //false
Your code is not working because you are comparing a string in your object to the numbers in your array.
category_id.indexOf(+n.category_id)
or
category_id.indexOf(parseInt(n.category_id, 10))
I don't see any reason to use _.first here? The rest of your filter statement looks good, altough you may swap the indexOf
call for _.contains:
var postDates = _.filter(goodDates, function(n){
return _.contains(category_id, n.category_id);
});