I have a data set as
var data1 = {values:[
{ X: "33", Y: 12 },
....
]};
var data2 = { values:[
{ X: "Jan", Y: 2 },
...
]};
I want to load appropriate data set by
$(document).ready(function() {
$(".test").click(function(){
var data = $(this).val() // the value will be data1 or data2
// how can I make the data a JSON equal to data1 or data2 instead of
// assigning the static value of $(this).val() to it.
}
});
How can I create the var data from the static value?
Don’t.
Have
data1,data2as properties of an object, and use the square bracket member operator to access them.Although if your
data1anddata2are global variables, you could access them the same way from thewindowobject.But an object like
datasetis still nicer than a bunch of globals.