Array.filter: max number of matches?
Array.prototype.some ?
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 :-)
Andrea Giammarchi wrote:
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 :-)
Making a copy and then mutating its length seems worse than using some, though.
The some method, as currently implemented, isn't truly better. Mathematically, I think Array.prototype.some would be equivalent to my suggested Array.prototype.filter(callback, thisObj, 1);
Alex, here is how you should use the Array#some in order to solve your problem:
var howManyConditions = 5; yourArray.some(function (value) { if (conditionSatisfied(value)) { --howManyConditions; } return !howManyConditions; });
in this case, as Brendan said, you don't even need a copy.
If the creation of new variables is the problem ...
yourArray.some(function(value){ if (conditionSatisfied(value)) { --this.breakLoop; } return !this.breakLoop; }, {breakLoop:5});
you can reuse both callback, if defined elsewhere once, and the object for those conditions ... well, you have many options, same is for every. One thing you might need is to save the last index somehow if needed for a splice later on.
br
On Tue, Nov 13, 2012 at 3:33 PM, Brendan Eich <brendan at mozilla.org> wrote:
Andrea Giammarchi wrote:
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 :-)
Making a copy and then mutating its length seems worse than using some, though.
Definitely, but it works without the slice(), too:
var filtereds, items, limit, k;
items = new Array(1000).join("-").split(""); limit = 3; k = 0;
filtereds = items.filter(function( val, i, orig ) { if ( true ) { if ( ++k === limit ) { orig.length = 0; } return true; } });
console.log( filtereds ); // [ '-', '-', '-' ]
be careful Rick, if no slice() then you might loose data from original Array. that's why I used slice ;-)
var a = [1, 2, 3]; a.filter(function(v,k,a){a.length=0}); alert(a); // "" empty
On Tue, Nov 13, 2012 at 7:04 PM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote:
be careful Rick, if no slice() then you might loose data from original Array. that's why I used slice ;-)
Of course, the non-slice way is like "to hell with everything" :)
var a = [1, 2, 3]; a.filter(function(v,k,a){a.length=0}); alert(a); // "" empty
I've actually been hunting through several large node.js projects trying to locate good use cases for this "limit count" arg, but I'm coming up empty handed.
Nb: A limit is consistent with String.prototype.split which has an optional limit in the range [0:4294967295] to avoid excessiveness.
(no great insight, but interesting?)
From: Alex Vincent Sent: Tuesday, November 13, 2012 2:07 AM To: es-discuss at mozilla.org Subject: Array.filter: max number of matches?
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?
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?