JavaScript 集合 API?

发布于 2024-10-05 00:04:52 字数 288 浏览 2 评论 0原文

我已经搜索了很长一段时间来寻找 JS 的 Collections API(列表、集合),令人惊讶的是我只能这样: http:// www.coffeeblack.org/work/jscollections/

这正是我一直在寻找的,但我想知道为什么 jQuery 不提供这个?我错过了什么?或者,也许我的搜索技术有多低效?

我知道数组支持 pop() 和 push(),但我需要 contains() 例如。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

森林迷了鹿 2024-10-12 00:04:52

您可以尝试 js_cols,这是一个 JavaScript 集合库。

You can try js_cols, a collections library for JavaScript.

∞梦里开花 2024-10-12 00:04:52

不能用jquery集合插件吗?

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

Can't you use the jquery collection plugin.

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

清眉祭 2024-10-12 00:04:52

jQuery 的主要焦点是 DOM。它不会也不应该试图满足所有人的需求,因此它没有太多的集合支持。

对于地图和集合,我想无耻地向您指出我自己实现这些的方向:http://code. google.com/p/jshashtable/

关于列表,Array 提供了您需要的大部分功能。与您可能需要的大多数数组方法一样,您可以在几行中组合一个 contains() 方法(其中大部分是为了处理 IE <= 8 缺乏对 的支持) indexOf() 方法):

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

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
南汐寒笙箫 2024-10-12 00:04:52

您还可以尝试 buckets,它拥有最常用的集合。

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

岁月打碎记忆 2024-10-12 00:04:52

由于 javascript 同时具有数组 [] 和关联数组 {},因此大多数数据结构的需求已经得到解决。数组解决了有序列表,通过数字索引的快速访问,而关联数组可以认为是无序的 hashmap,解决了通过字符串键的快速访问。

对我来说,这满足了我 95% 的数据结构需求。

Since javascript have both arrays [] and associative arrays {}, most needs for datstructures are already solved. The array solves the ordered list, fast access by numeric index whilst the associative array can be considered a unordered hashmap and solves the fast access by string keys.

For me that covers 95% of my data structure needs.

长梦不多时 2024-10-12 00:04:52

如果你有空闲时间,你可以结帐。
https://github.com/somnathpanja/jscollection

If you get some free time you can checkout.
https://github.com/somnathpanja/jscollection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文