jQuery 有 hashtable 类型的可变数据结构吗?

发布于 2024-11-05 00:21:46 字数 414 浏览 0 评论 0原文

伪代码可能是展示我想要在 jQuery 中执行的操作的最佳方式:

$(selector).each(function() {
    // pull data from LI tags or whatever, store in variables (imgURL, tagline, summary)
    $someDataStructure.add({imgURL, tagline, summary});
});

然后稍后...参考结构

 $someDataStructure.each(function() {
     var x = $someDataStructure['imgURL'];
     // etc.
 });

关于如何执行此操作有什么建议吗?

Pseudo-code is probably the best way to show what I'm looking to do in jQuery:

$(selector).each(function() {
    // pull data from LI tags or whatever, store in variables (imgURL, tagline, summary)
    $someDataStructure.add({imgURL, tagline, summary});
});

Then later... reference the structure

 $someDataStructure.each(function() {
     var x = $someDataStructure['imgURL'];
     // etc.
 });

Any suggestions on how I could go about doing this?

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

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

发布评论

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

评论(2

乙白 2024-11-12 00:21:46

jQuery 就是 JavaScript,而 JavaScript 有对象。您可以像 C++ 映射或 C# 字典一样使用 JS 对象。例如,您可以创建一个对象数组并具有可以命名的索引:

var liTags = [];

$(selector).each(function() {
   // pull data from LI tags or whatever
   //store in variables (imgURL, tagline, summary)
   liTags.push({'imgURL': imgURL, 'tagline': tagline, 'summary': summary});
});

然后:

for(int i = 0; i < liTags.length; ++i) {
   var imgURL = liTags[i]['imgURL']; //or liTags[i].imgURL;
}

jQuery is just JavaScript, and JavaScript has objects. You can use JS Objects similarly to a C++ map or C# Dictionary. For example, you can create an array of objects and have indices that you can name:

var liTags = [];

$(selector).each(function() {
   // pull data from LI tags or whatever
   //store in variables (imgURL, tagline, summary)
   liTags.push({'imgURL': imgURL, 'tagline': tagline, 'summary': summary});
});

Then later:

for(int i = 0; i < liTags.length; ++i) {
   var imgURL = liTags[i]['imgURL']; //or liTags[i].imgURL;
}
心作怪 2024-11-12 00:21:46

您实际上可以使用 data api 将数据附加到 jquery 元素,

所以在行:

$(selector).each(function() {
    /* I suppose here you have three available variables named:
       imgURL, tagline, summary
        that you got from the element itself or from somewhere else
     */
    $(this).data({imgURL: imgURL, tagline: tagline, summary:summary })
    /* you could also write it {'imgURL': imgURL, etc. } if it looks clearer: the first is the hashtable key, the second a variable containing the value */
}

然后从每个 jquery 元素中检索它!

$(selector).data()

$(selector).data('imgURL')

用于将“哈希表”附加到元素。

在 javascript 中,有“关联数组”(哈希表)。

您可以将它们写为:
myarray = {key:'value'}

您可以

根据需要通过以下方式访问它们:myarray['attribute']myarray.attribute

附言。请注意,我没有在那里测试代码,它可能需要一些小的调整

You can actually attach data to a jquery element using the data api

So do something in the lines of:

$(selector).each(function() {
    /* I suppose here you have three available variables named:
       imgURL, tagline, summary
        that you got from the element itself or from somewhere else
     */
    $(this).data({imgURL: imgURL, tagline: tagline, summary:summary })
    /* you could also write it {'imgURL': imgURL, etc. } if it looks clearer: the first is the hashtable key, the second a variable containing the value */
}

and then just retrieve it from each jquery element!

$(selector).data()

or

$(selector).data('imgURL')

That's for attaching an 'hashtable' to an element.

In javascript you have 'associative arrays' (hashtables).

You can write them as:
myarray = {key:'value'}

and you can access them as:

myarray['attribute'] or myarray.attribute as you prefer

PS. please note that I did not test the code up there, it might require some minor tweaks

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