Created
October 25, 2016 21:00
-
-
Save dbalduini/f62a3eb77275354db3a740419a11b167 to your computer and use it in GitHub Desktop.
Underscore filter function which returns the indexes instead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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