Ticket #8689 (closed enhancement: worksforme)
Add .collect() function that creates new collection out of existing one
| Reported by: | igor.nikolaev@… | Owned by: | |
|---|---|---|---|
| Priority: | undecided | Milestone: | 1.next |
| Component: | unfiled | Version: | 1.5.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
I would like to contribute the following function to the jQuery core:
(function($) {
$.fn.collect = function(callback) {
if (typeof(callback) == "function") {
var collection = [];
$(this).each(function() {
var item = callback.apply(this);
if (item)
collection.push(item);
});
return collection;
}
return this;
}
})(jQuery);
This function lets create a collection out of other collection with capability to filter initial collection. Consider the following example:
var colors = $("input.color").collect(function(){return this.name});
This example creates new collection of strings containing values of name attribute of inputs returned by 'input.color' selector.
It is also possible to filter collection:
var colors = $("input.color").collect(function(){if (this.name != "Blue") return this.name});
Change History
comment:1 follow-up: ↓ 3 Changed 2 years ago by dmethvin
- Status changed from new to closed
- Resolution set to worksforme
comment:2 Changed 2 years ago by igor.nikolaev@…
No, .filter() method returns filtered collection of the same objects:
var input = $(".input").filter(function() {return true})
This still returns the collections of input objects.
And intension of .collect() method is to create a completely different collection with items of different type that initial one:
var colors = $("input.color").collect(function(){return this.name});
This will create an array of strings which containt the contents of name attribute of input.
Else I would have to write the following ugly code:
var colors = [];
$("input.color").each(function(){colors.push(this.name)});
My use case is following:
- find inputs by selector
- get names of inputs that match selector as a collection
comment:3 in reply to: ↑ 1 Changed 2 years ago by igor.nikolaev@…
I have created an example on jsFiddle: http://jsfiddle.net/B8jxU/6/
Replying to dmethvin:
That is describing the .filter() method, as far as I can tell. All jQuery methods that filter or select new elements create a new jQuery object, rather than modifying the old one. The implementation is flawed because it doesn't return a jQuery object.
If you'd like to ask more questions, please open a thread on the Using jQuery section of forum.jquery.com. Thanks!
comment:4 Changed 2 years ago by dmethvin
You can do that with .map. http://jsfiddle.net/dmethvin/B8jxU/7/
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

That is describing the .filter() method, as far as I can tell. All jQuery methods that filter or select new elements create a new jQuery object, rather than modifying the old one. The implementation is flawed because it doesn't return a jQuery object.
If you'd like to ask more questions, please open a thread on the Using jQuery section of forum.jquery.com. Thanks!