I want to return the index of the first element satisfying the unary predicate.
Example:
[1,2,3,4,5,6,7].indexOf((x) => x % 3 === 0) // returns 2
Is there such a function? The alternative I was going to use was
[1,2,3,4,5,6,7].reduce((retval,curelem,idx) =>
{
if(curelem % 3 === 0 && retval === undefined)
retval = idx;
return retval;
}, undefined);
but of course it will be less efficient as it does not stop the repetition through the array after it has found the element.
source
share