If you want to stay underlined to make your predicate function more flexible, here are 2 ideas.
Method 1
Since the predicate for _.find gets both the value and the index of the element, you can use the side effect to extract the index, for example:
var idx; _.find(tv, function(voteItem, voteIdx){ if(voteItem.id == voteID){ idx = voteIdx; return true;}; });
Method 2
Considering the source of the underscore, this is the _.find way:
_.find = _.detect = function(obj, predicate, context) { var result; any(obj, function(value, index, list) { if (predicate.call(context, value, index, list)) { result = value; return true; } }); return result; };
To make this findIndex function, simply replace the line result = value; on result = index; . This is the same idea as the first method. I included it to emphasize that underlining uses a side effect to implement _.find .
lastoneisbearfood Aug 26 '14 at 10:51 2014-08-26 10:51
source share