Array.filter: max number of matches?

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Nov 12 18:20:37 PST 2012


wait, I might have misunderstood your problem ... so you want to stop
iterating, right ?

When that is the case, you can simply drop the iteration like this:

myArray.slice().filter(function (value, index, original) {
  // do your stuff
  if (conditionSatisfied) {
    original.length = 0;
  }
});

there, you can break the iteration as easy as that :-)



On Mon, Nov 12, 2012 at 6:07 PM, Alex Vincent <ajvincent at gmail.com> wrote:

> I've been wondering for a while if it would be a good idea to add an
> argument to Array.prototype.filter, for a maximum number of matches.  For
> example, if I have a thousand-element array and I only want three matches,
> it might make sense to truncate the search after the third match.
>
> Put another way:  if I want an intersection between two arrays treated as
> sets, and one array has only three members, then once I've found matches
> for those three, it really doesn't make sense to keep iterating.
>
> Thoughts, anyone?
>
> --
> "The first step in confirming there is a bug in someone else's work is
> confirming there are no bugs in your own."
> -- Alexander J. Vincent, June 30, 2001
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121112/596baefb/attachment.html>


More information about the es-discuss mailing list