Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Collections API?

I've searched quite a while for a Collections API (list, set) for JS and surprisingly I could only this: http://www.coffeeblack.org/work/jscollections/

This is exactly what I was looking for, but I'm wondering why doesn't jQuery provide that? What am I missing on? Or, perhaps, how ineffective are my searching techniques?

I know that arrays support pop() and push(), but I need contains() for example.

like image 982
simpatico Avatar asked Nov 28 '10 08:11

simpatico


4 Answers

You can try js_cols, a collections library for JavaScript.

like image 192
Thomas Avatar answered Oct 24 '22 01:10

Thomas


Can't you use the jquery collection plugin.

http://plugins.jquery.com/project/Collection

like image 45
Aim Kai Avatar answered Oct 24 '22 03:10

Aim Kai


jQuery's primary focus is the DOM. It doesn't and shouldn't try and be all things to all people, so it doesn't have much in the way of collections support.

For maps and sets, I'd like to shamelessly point you in the direction of my own implementations of these: http://code.google.com/p/jshashtable/

Regarding lists, Array provides much of what you need. Like most methods you might want for arrays, you can knock together a contains() method in a few lines (most of which are to deal with IE <= 8's lack of support for the indexOf() method):

Array.prototype.contains = Array.prototype.indexOf ?
    function(val) {
        return this.indexOf(val) > -1;
    } :
    function(val) {
        var i = this.length;
        while (i--) {
            if (this[i] === val) {
                return true;
            }
        }
        return false;
    };

["a", "b", "c"].contains("a"); // true
like image 35
Tim Down Avatar answered Oct 24 '22 01:10

Tim Down


You can also try buckets, it has the most used collections.

like image 20
Daniel Avatar answered Oct 24 '22 03:10

Daniel