Bug Tracker

Ticket #8689 (closed enhancement: worksforme)

Opened 4 years ago

Last modified 4 years ago

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 4 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to worksforme

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:2 Changed 4 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:

  1. find inputs by selector
  2. get names of inputs that match selector as a collection

comment:3 in reply to: ↑ 1 Changed 4 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 4 years ago by dmethvin

You can do that with .map.  http://jsfiddle.net/dmethvin/B8jxU/7/

Note: See TracTickets for help on using tickets.