admin管理员组文章数量:1391987
function get_event_ids_from_dom()
{
var event_ids = {};
$.each(
$("td.ms-cal-defaultbgcolor 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.
function get_event_ids_from_dom()
{
var event_ids = {};
$.each(
$("td.ms-cal-defaultbgcolor 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.
Share Improve this question asked Jun 23, 2011 at 14:10 zer0c00lzer0c00l 3031 gold badge3 silver badges14 bronze badges 4- @Ivaylo Slavov No it does not work. I am getting TypeError: event_ids[id].push is not a function, In firebug. Could you please check again? – zer0c00l Commented Jun 23, 2011 at 14:19
-
1
Your example is not valid JavaScript:
**event_ids.id.push(this);**
? And you shouldn't use theString
class directly. It's just there to decorate string values with methods. When you call methods on a string value JavaScript is simply calling the methods on the prototype ofString
in the context of the string value. You're going to run into problems if you're using an object as a string instead of just a string. – Bjorn Commented Jun 23, 2011 at 14:24 - @apphacker The value variable will have something like this "Business%20Development/Lists/Calendar/DispForm.aspx?ID=907". But it is in object form. I want to convert it into string to process it. Is it possible without using String class? – zer0c00l Commented Jun 23, 2011 at 14:33
- @apphacker got it from Ivaylo's answer. changed my code to value.toString() , Thanks for the info :-) – zer0c00l Commented Jun 23, 2011 at 14:43
3 Answers
Reset to default 2Try 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 ment 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
本文标签: jqueryDereferencing a variable to its value to use in another function javascriptStack Overflow
版权声明:本文标题:jquery - Dereferencing a variable to its value to use in another function javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744757501a2623550.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论