Does jQuery offer an equivalent iterator function to “collect” and “map” in Prototype? These functions return the result of applying an iterator to each element: http://www.prototypejs.org/api/enumerable/collect
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s a “map()” but no “reduce()” or “collect()”. The jQuery people have a considerable history of being resistant to adding a “reduce()” in the absence of clear benefit to the jQuery core code itself.
You can pick up and extend simple implementations of functions like that from the Functional.js library.
Also, be warned that the jQuery “map” facility has a couple of questionable features that are handy at times but a serious pain at others. Specifically, the callback function passed in to “map()” returns a value for the result array, as you might expect. However there are two special cases:
nullorundefined, then nothing is added to the result array; in other words, it’s interpreted as a request to skip that element. This makes it hard to have a result array with explicitlynullentries.Both those treats are handy sometimes but after spending half a day discovering the second one (yes, it’s documented) I’ve got some painful memories 🙂