How to create a hash object / array using jquery?

I know there is a Hash () object in the structure of the Javascript prototype, but is there anything in jQuery?

As I would like to stick to one javascript framework, instead of mixing Frame Prototype and JQuery framework and using at the same time, as I am worried that there will be conflicts and create side effects.

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

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);
    }
}
+5
source share
5 answers

, 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);
    }
}
+5

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';
        });        
    }
}
+4

, 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;
    }
}
+3

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;
    }
}
0

jQuery, , Hash.

? 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;
    }
}
0

All Articles