On Sun, Mar 3, 2013 at 9:38 PM, Brandon Benvie <[email protected]> wrote:
> I was going to say...
>
> indexOf with a callback is the best thing since sliced bread.
>
Is this +1 to findIndex?
I ask because:
var a = function() {}, array = [a];
array.indexOf(a); // 0
Rick
>
> On Mar 3, 2013, at 5:26 PM, Rick Waldron <[email protected]> wrote:
>
>
>
>
> On Sun, Mar 3, 2013 at 5:33 PM, Brendan Eich <[email protected]> wrote:
>
>> Bjoern Hoehrmann wrote:
>>
>>> * David Bruant wrote:
>>>
>>>> I've found myself multiple times in a situation where I needed the index
>>>> of the first element responding to some conditions. I solved it the
>>>> following way:
>>>>
>>>> var index;
>>>> array.some(function(e, i){
>>>> if(someCondition(e)){
>>>> index = i;
>>>> return false;
>>>> }
>>>>
>>>> return true;
>>>> })
>>>>
>>>
>>> It's usually a bad idea to trigger side-effects from such callbacks. The
>>> proper solution here would be having a suitable method available, like
>>>
>>> var index = array.findIndex(someCondition)**;
>>>
>>> as it is called in Haskell and C#.
>>>
>>
>> Hi Bjoern, findIndex is indeed a missing Array generic. I'll see if we
>> can get it on the agenda for the next TC39 meeting (coming up quickly,
>> mid-March). Thanks,
>>
>
> Despite my previous message with a solution that relied on
> array.reduce(...), the benefit of stopping the iteration (once the
> predicate returns true) is objectively compelling.
>
> Rick
>
>
>>
>> /be
>>
>> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss