Side navigation
#8689 closed enhancement (worksforme)
Opened March 29, 2011 09:31AM UTC
Closed March 29, 2011 12:19PM UTC
Last modified March 29, 2011 03:14PM UTC
Add .collect() function that creates new collection out of existing one
Reported by: | igor.nikolaev@nikisoft.ru | Owned by: | |
---|---|---|---|
Priority: | undecided | Milestone: | 1.next |
Component: | unfiled | Version: | 1.5.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
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});
Attachments (0)
Change History (4)
Changed March 29, 2011 12:19PM UTC by comment:1
resolution: | → worksforme |
---|---|
status: | new → closed |
Changed March 29, 2011 12:46PM UTC by comment:2
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:
1. find inputs by selector
2. get names of inputs that match selector as a collection
Changed March 29, 2011 02:54PM UTC by comment:3
I have created an example on jsFiddle: http://jsfiddle.net/B8jxU/6/
Replying to [comment:1 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!
Changed March 29, 2011 03:14PM UTC by comment:4
You can do that with .map
. http://jsfiddle.net/dmethvin/B8jxU/7/
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!