New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can.List.filter #683

Closed
stevenvachon opened this Issue Jan 20, 2014 · 3 comments

Comments

Projects
None yet
4 participants
@stevenvachon
Contributor

stevenvachon commented Jan 20, 2014

For times when removing multiple indexes, looping in reverse order is fastest. I can't think of any other time that I use reverse loops, so instead of requesting a reverse option in .forEach(), a request is being made for a .filter() method.

list.filter( function(el, index, list)
{
    return el.attr("something") !== "poop";
}, this);

@param {function()} callback A function to call with each element of the list. Returning false will remove the index.
@param {Object} thisArg The object to use as this inside the callback.


Should we also add a can.filter()?

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Jan 21, 2014

Contributor

Thanks for the suggestion. I'm gonna play devils advocate here:

You don't need to loop in reverse order to remove multiple indexes, you can use a while loop.

Calling .reverse() isn't that bad. It is pretty darn fast and won't change the limiting function of your code.

I'd rather just add a reject method to lists that would remove those items like underscore's reject

Contributor

justinbmeyer commented Jan 21, 2014

Thanks for the suggestion. I'm gonna play devils advocate here:

You don't need to loop in reverse order to remove multiple indexes, you can use a while loop.

Calling .reverse() isn't that bad. It is pretty darn fast and won't change the limiting function of your code.

I'd rather just add a reject method to lists that would remove those items like underscore's reject

@stevenvachon

This comment has been minimized.

Show comment
Hide comment
@stevenvachon

stevenvachon Jan 21, 2014

Contributor

Even with a while() loop, going backwards is still faster than forwards because setting the iterator back a value isn't necessary. Great idea about the .reject() method. Original post updated.

Contributor

stevenvachon commented Jan 21, 2014

Even with a while() loop, going backwards is still faster than forwards because setting the iterator back a value isn't necessary. Great idea about the .reject() method. Original post updated.

@stevenvachon

This comment has been minimized.

Show comment
Hide comment
@stevenvachon

stevenvachon Jan 22, 2014

Contributor

After thinking a little more, I think that a .filter() would be better as its name suggests working both for "keeping" as well as "removing".

Contributor

stevenvachon commented Jan 22, 2014

After thinking a little more, I think that a .filter() would be better as its name suggests working both for "keeping" as well as "removing".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment