Javascript 如何使用 jquery 创建哈希对象/数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3006984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:52:21  来源:igfitidea点击:

How to create Hash object/array using jquery?

javascriptjqueryhashprototypejs

提问by Patrick

I know there is a Hash() object in the Javascript prototype framework, but is there anything in Jquery like this?

我知道在 Javascript 原型框架中有一个 Hash() 对象,但是在 Jquery 中有这样的东西吗?

As I would like to stick with one javascript framework, rather than mixing the Prototype Frame work and the JQuery framework and use at the same time, as I worry there will be conflict and create side-effects.

因为我想坚持使用一个 javascript 框架,而不是将 Prototype Frame 工作和 JQuery 框架混合在一起使用,因为我担心会发生冲突并产生副作用。

So my question is: how to create Hash object/array using jquery?

所以我的问题是:如何使用 jquery 创建 Hash 对象/数组?

Here is my function:

这是我的功能:

/* prototype framework, I want to change this to jQuery! */
var starSaves = new Hash();

function myHover(id, pos)
{
    var starStrip = $('star_strip_' + id);    
    if (starSaves.keys().indexOf(id) == -1)
    {
        var starSave = new Array();
        var imgs = starStrip.select("img")
        alert(imgs);
        for (var i = 0; i < imgs.length; i++)
        {
            starSave[starSave.length] = imgs[i].src;
            if (i < pos)
                imgs[i].src = "/images/star_1.gif";
            else
                imgs[i].src = "/images/star_0.gif";

        }
        starSaves.set(id, starSave);
    }
}

回答by Tim Down

There's a standalone hash table implementation called jshashtable(full disclosure: I wrote it). Using it, your code would be:

有一个名为jshashtable的独立哈希表实现(完全披露:我写的)。使用它,您的代码将是:

var starSaves = new Hashtable();

function myHover(id, pos)
{
    var starStrip = $('star_strip_' + id);
    if (!starSaves.containsKey(id))
    {
        var starSave = new Array();
        var imgs = starStrip.select("img")
        alert(imgs);
        for (var i = 0; i < imgs.length; i++)
        {
            starSave[starSave.length] = imgs[i].src;
            if (i < pos)
                imgs[i].src = "/images/star_1.gif";
            else
                imgs[i].src = "/images/star_0.gif";

        }
        starSaves.put(id, starSave);
    }
}

回答by Matt

A basic Hashcan be achieved with basic JavaScript, not jQuery (I know that Prototype's Hash adds extra functionality, but you don't use it here).

基本的Hash可以用基本的 JavaScript 来实现,而不是 jQuery(我知道 Prototype 的 Hash 添加了额外的功能,但你不会在这里使用它)。

var starSaves = {};

function myHover(id, pos)
{
    if (!starSaves.hasOwnProperty(id)) {
        var starSave = starSaves[id] = [];

        $('#star_strip_' + id + ' img').attr('src', function (index, current) {
           starSave.push(current);

           return (index < pos) ? '/images/star_1.gif' : '/images/star_0.gif';
        });        
    }
}

回答by Amadu Bah

I think you don't need jQuery for this.

我认为你不需要jQuery。

var hashtable = {
    hash: {},
    exists: function(key){
       return this.hash.hasOwnProperty(key);
    },
    get: function(key){
        return this.exists(key)?this.hash[key]:null;
    },
    put: function(key, value){
        this.hash[key] = value;
    }
}

回答by Nalum

There is no prototype Hash equivalent in jQuery that I know of.

我所知道的 jQuery 中没有原型哈希等价物。

Could the following be of any use to you? using jQuery

以下内容对您有用吗?使用 jQuery

var starSaves = {};

function myHover(id,pos)
{
    var starStrip = $('.star_strip_' + id);
    if(!starSaves[id])
    {
        var starSave = [];
        starStrip.each(function(index,element){
            starSave[index] = $(element).attr('src');
            $(element).attr('src', index < pos ? '/images/star_1.gif' : '/images/star_0.gif');
        });
        starSaves[id] = starSave;
    }
}

回答by jdcantrell

Glancing at the jQuery documentation I'm not seeing anything that specifically for this. But you can easily do this with just a regular javascript object:

浏览 jQuery 文档,我没有看到任何专门针对此的内容。但是你可以很容易地用一个普通的 javascript 对象来做到这一点:

var starSaves = {};
function myHover(id, pos)
{
    var starStrip = $('star_strip_' + id);    
    if (typeof starSaves[id] == 'undefined')
    {
        var starSave = new Array();
        var imgs = starStrip.select("img")
        alert(imgs);
        for (var i = 0; i < imgs.length; i++)
        {
            starSave[starSave.length] = imgs[i].src;
            if (i < pos)
                imgs[i].src = "/images/star_1.gif";
            else
                imgs[i].src = "/images/star_0.gif";

        }
        starSaves[id] = starSave;
    }
}