Throwing StopIteration in array extras to stop the iteration

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun Mar 3 15:15:42 PST 2013


hacky but easy to solve reusing once a RegExp

var i = [1,2,3].some(function(v, i){
  return v === 2 && this.test(i);
}, /\d+/) && RegExp['$&'];

alert(i); // 1

no extra loop, no global anything, just an extra object needed, the RegExp,
actually something you could create once in your closure and recycle every
time you need.

br




On Sun, Mar 3, 2013 at 2:33 PM, Brendan Eich <brendan at mozilla.com> 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,
>
> /be
>
> ______________________________**_________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130303/b2457794/attachment-0001.html>


More information about the es-discuss mailing list