开发者

Dereferencing a variable to its value to use in another function javascript

function get_event_ids_from_dom()
{
    var event_ids = {};
    $.each(
    $("td.ms-cal-d开发者_JS百科efaultbgcolor a"),
        function(index,value){
             var str = new String(value); 
             var id = str.substring(str.indexOf('=')+1,str.length);
             if(typeof(event_ids[id]) == "undefined")
             {
                event_ids[id] = this;
            }
            else
            {
                **event_ids.id.push(this);**

            }
        }
     )
        return event_ids;
}

In above javascript event_ids is a hashtable. I am trying to assign values to this hashtable.

A hashtable can be added with multiple values using "hashtable.key.push(value)". I am trying to do this using event_ids.id.push(this); in the above code.

I have declared "id" as a variable in the code. The problem is, I am not able to dereference variable "id" to its value.

Is this possible in jquery/javascript?

Example use of hashtable:

event_ids = {};
event_ids["1"]= 'John';
event_ids.1.push('Julie');

The above example would add john and julie to hash table.


Try this instead:

function get_event_ids_from_dom() {
    var event_ids = {};
    $.each(
        $("td.ms-cal-defaultbgcolor a"),
        function(index,value){
            var str = value.toString(); 
            var id = str.substring((str.indexOf('=') + 1), str.length);
            if(typeof(event_ids[id]) == "undefined") {
                event_ids[id] = [];
            }
            event_ids[id].push(this);
        });
    return event_ids;
}

Please, note that while object["id"] is the same as object.id, object[id] is not.


Nicola almost had it:

if(typeof(event_ids[id]) == "undefined") {
  event_ids[id] = [];
}
event_ids[id].push(this);

Also please read the comment I left for your question.


In my opinion event_ids is an object (there are no hastables in javascript, just either indexed arrays or objects). What you are tring to do is using push (an array method) on something that is not an array so i think you must change something:

you could try:

         if(typeof(event_ids[id]) == "undefined")
         {
            event_ids[id] = [];// the property id of object event_ids is an array
            event_ids[id].push(this);
        }
        else
        {
            event_ids[id].push(this);

        }

It should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜