Skip to content

Instantly share code, notes, and snippets.

@dbalduini
Created October 25, 2016 21:00
Show Gist options
  • Save dbalduini/f62a3eb77275354db3a740419a11b167 to your computer and use it in GitHub Desktop.
Save dbalduini/f62a3eb77275354db3a740419a11b167 to your computer and use it in GitHub Desktop.
Underscore filter function which returns the indexes instead
const _ = require('underscore')
function filterIndexes(collection, selector) {
var results = [],
predicate = _.matcher(selector);
// Works just like _.filter, but returns the indexes instead
_.each(collection, function (value, index, list) {
if (predicate(value, index, list)) {
results.push(index);
}
});
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment