Topic: Javascript - string as variable name? (Page 1 of 1) |
|
|---|---|
|
Lunatic (VI) Inmate From: under the bed |
posted 11-02-2010 21:12
Hey guys. |
|
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 11-02-2010 22:43
say you have an object with id string like this |
|
Paranoid (IV) Inmate From: Norway |
posted 11-03-2010 00:13
window[variableName] works for variables declared in the global namespace ( the window object ): in other words, for variables declared outside of a function or inside a function but without the var keyword. |
|
Lunatic (VI) Inmate From: under the bed |
posted 11-03-2010 00:43
Ok, well, removing the 'var' accomplished what I need for the moment. |
|
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 11-03-2010 02:19
if you made an array with the ids as keys you could call them like this |
|
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 11-03-2010 08:00
I tested it and it seemed to work code: var chart, id='';
var objs=[]
set1 = {name:'Set 1',data:[1,2,5,8,9,6,3,4,7,8],color:'#c66',id:'set1'};
set2 = {name:'Set 2',data:[5,8,9,3,1,4,7,8,9,6],color:'#6c6',id:'set2'};
set3 = {name:'Set 3',data:[8,5,7,4,2,3,6,9,4,1],color:'#666',id:'set3'};
objs[set1.id]=set1
objs[set2.id]=set2
objs[set3.id]=set3
//---------------------------------------
chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
series: [set1]
});
//---------------------------------------
$("#controls input").each(function(){
$(this).click(function(){
var sID = $(this).attr('id');
if($(this).attr('checked') == true){
chart.addSeries(objs[$(this).attr('id')]); //don't know how to make this reference the variable above...?
}
else{
chart.get($(this).attr('id')).remove();
}
}); |
|
Paranoid (IV) Inmate From: Norway |
posted 11-03-2010 12:31
Personally I'd put the data sets either as a TABLE element in the markup, for accessibility reasons, or in private variables to prevent messing up with them. code: var objs=[]
set1 = {name:'Set 1',data:[1,2,5,8,9,6,3,4,7,8],color:'#c66',id:'set1'};
/* ... */
objs[set1.id]=set1That's one convoluted way of doing: code: var objs={};
objs.set1 = {name:'Set 1',data:[1,2,5,8,9,6,3,4,7,8],color:'#c66'};or a more direct object declaration like the one in the first jsfiddle link. |
|
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 11-03-2010 18:29
Arrays are objects, right? |
|
Paranoid (IV) Inmate From: Norway |
posted 11-03-2010 18:41
|
|
Lunatic (VI) Inmate From: under the bed |
posted 11-03-2010 23:56
I have to admit to a lot of what you guys say being lost on me code: var dataSets = {
set1: {name:'Set 1',data:[1,2,5,8,9,6,3,4,7,8],color:'#c66',id:'set1'},
set2: {name:'Set 2',data:[5,8,9,3,1,4,7,8,9,6],color:'#6c6',id:'set2'},
set3: {name:'Set 3',data:[8,5,7,4,2,3,6,9,4,1],color:'#666',id:'set3'}
}
|
|
Paranoid (IV) Inmate From: Norway |
posted 11-04-2010 08:50
|
|
Lunatic (VI) Inmate From: under the bed |
posted 11-05-2010 16:57
Ok, cool. Thanks |
|
Paranoid (IV) Inmate From: Norway |
posted 11-05-2010 17:51
if you've got a debugging tool in your browser, console.log( anyVariable ); should output just what you want. |
|
Obsessive-Compulsive (I) Inmate From: |
posted 05-31-2011 10:59
Edit TP: spam removed
|