On Tue, Nov 13, 2012 at 7:04 PM, Andrea Giammarchi <
[email protected]> 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.
Rick
>
>
> On Tue, Nov 13, 2012 at 3:38 PM, Rick Waldron <[email protected]>wrote:
>
>>
>>
>>
>> On Tue, Nov 13, 2012 at 3:33 PM, Brendan Eich <[email protected]>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 );
>> // [ '-', '-', '-' ]
>>
>>
>> Rick
>>
>>
>>
>>>
>>>
>>> /be
>>>
>>>
>>> ______________________________**_________________
>>> es-discuss mailing list
>>> [email protected]
>>> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>>>
>>
>>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss