I don't think it thinks this is a variable.
I have a global variable dummy1, dummy2, dummy3, etc. I keep getting undefined when I do console.log(dummy1).
I want to append the 1,2,3 to the word dummy and save whatever I have into my global. This isn't working and I don't even think it is efficient but not sure what to do?
for(var i = 1; i<3; i++){
if(numberSelect == i){
window["dummy"[i]]=numberSelect;
}
}
Change to:
window["dummy" + i] = numberSelect;
Try out this approach.
window.CustomStorage = {};
window.CustomStorage["dummy"+[i]]=numberSelect;
And you can retrieve it like
window.CustomStorage.dummy1 or simply CustomStorage.dummy1
So basic idea is you save all your dynamic stuff into a object called CustomStorage and retrieve it by using the property name. ie dummy1, dummy2 etc
Related
I am trying to find a better solution for adding objects to an array. The box objects are from a separate file and are pushed to the array one line at a time in a different file, as such:
function loadCols(){
collisionPoints.push(box1);
collisionPoints.push(box2);
collisionPoints.push(box3);
collisionPoints.push(box4);
collisionPoints.push(box5);
collisionPoints.push(box6);
collisionPoints.push(box7);
collisionPoints.push(box8);
collisionPoints.push(box9);
collisionPoints.push(box10);
};
I have tried using a for loop and concatenating the string "box" + i but this didn't work.
I also tried adding them to an array in the file where the objects are created but I was not able to find a way of passing the array to the main file. Although this works I'm hoping there is a cleaner solution. Any help would be appreciated, cheers.
You can get a variable from it's string name, by using the window object.
function loadCols(){
for (var i=1; i<=numberOfBoxVars; i++) {
collisionPoints.push(window["box" + i]);
}
}
Alternatively, if your variable are defined within a closure and your loadCols function is defined within the same closure, you can use the "this" keyword in place of the window object.
(function() {
var box1 = "1";
var box2 = "2";
...
function loadCols(){
for (var i=1; i<=numberOfBoxVars; i++) {
collisionPoints.push(this["box" + i]);
}
}
});
If I understand you correctly you are looking for a way to use dynamic variables in a for-loop. If box1 and so on are global variables you can get them dynamically by accessing them as property of window:
window['box'+i]
See here: Use dynamic variable names in JavaScript
If you send all the objects in a JSON array you could just do this:
var array = JSON.parse(boxesarray);
for(var i = 0;i< array.length; i++) {
collisionPoints.push(array[i]);
}
But it would require you sending all the boxes in an array, if this is not possible please post code as to why it isn't and i will adapt my anwser.
I need to combine 2 variable names, but it must be in a for loop. Also I need to get the variable from a php file to know how long the loop must be.
I have while loop in php file which count how many objects I have and make "$k++" for every object. After while loop I have variable what store count of objects $galak=$k-1 ... I need to make javascript something like:
document.getElementById("galak");
var i;
for (i = 1; i < galak; i++) {
var decimalMask.i = new InputMask(JST_MASK_DECIMAL, "rdcena".i);
}
After loop "for" there must be decimalMask1,decimalMask2,decimalMask3 etc. and rdcena1,rdcena2,rdcena3 etc.
So question is how to add variable "i" value to other variables decimalMask and rdcena?!
Try to concatenate them. Replace . with +. + is the concatenation operator in javascript. And make decimalMask an array.
decimalMask[i] = new InputMask(JST_MASK_DECIMAL, "rdcena" + i);
Then it can be accessed as decimalMask[1] or decimalMask[2]...
In a situation where I have something like this code:
var variableNames=["thisMonth", "thisDay"];
var variableValues=["February", 17];
Is there any way I could go through the array and initiate variables with their corresponding values? I've tried something like
for(var i=0;i<variableNames.length;i++){
eval("var "+variableNames[i]+"="+variableValues[i]+";");
}
But I'm not getting any reults. Is eval not able to define variables, or are there other problems that exist? Any solution would be greatly appreciated.
You need to assign the variables on an object. If you want to create global variables the following code should work:
for (var i=0; i<variableNames.length; i++) {
window[variableNames[i]] = variableValues[i];
}
//test
console.log(thisMonth); //"February"
Here you go. You missed a couple of quotes at "='" + variableValues[i] + "';");:
var variableNames=["thisMonth", "thisDay"];
var variableValues=["February", 17];
for(var i=0;i<variableNames.length;i++){
eval("var "+variableNames[i]+"='"+variableValues[i]+"';");
}
With that correction however, I would warn you against using it cause it's a very wrong way of doing it.
Use Objects, as most here mention.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dynamic object property name
I have an object like this
var localAppConfig = {
wallet:0,
paySomeone:0,
payBills:0,
accounts:0,
moveMoney:0,
alerts:0,
offers:0,
checkIn:0
};
I want to set value 1 for particular elements within this localAppConfig
Which element needs to be set is retrieved from the json - which arrives from the server.
say, I want to set value = 1 for wallet, paySomeone, payBills, alerts, offers, checkIn
These are retirved from the json like
for(var i=0;i<len;i++){
var name = list[i].handle;
var accessor = eval('localAppConfig.'+name);
eval('localAppConfig.'+name)=1;
}
var name contains name of the element and I am able to access its value correctly,
How can I set the value using javascript?
I tried accessor=1 but its not working.
Thanks :)
Anyhow: try this on for size:
localAppConfig[name] = 1;//where name is a variable of choice, it's value will be used as the propertyname
And again:
-When all you have is the eval hammer, everything looks like your thumb.
–Brendan Eich in response to: we should encourage use of eval() rather than the Function constructor for dynamic creation of functions.
But why use a list of the properties? you might as well use a for...in loop: in case of objects, like yours, it's perfectly all right. As long as you check .hasOwnProperty, too:
for (var prop in localAppConfig)
{
if (localAppConfig.hasOwnProperty(name))
{
//set, delete... do whatever
}
}
You should do this instead:
var accessor = localAppConfig[name];
localAppConfig[name] = 1;
Try localAppConfig[name] = 1;
It's just a javascript object, no need to use eval() on it.
Don't use eval(). You can add a property by referencing the index of that value within your object. If it doesn't exist then it will be created, otherwise the value will be overridden.
Try the following instead:
// localAppConfig[list[i].handle] = 1;
for(var i=0;i<len;i++){
localAppConfig[list[i].handle] = 1;
}
Or if you intend to reference the variable in another place then set a variable with the value of list[i].handle:
for(var i=0;i<len;i++){
var name = list[i].handle;
var accessor = localAppConfig[name];
localAppConfig[name] = 1;
}
I want to create a new variable in javascript but it's name should made of a stale part and a variable one like this:
tab_counter = 1;
var editor + tab_counter = blabla
well i want the new variable name to be in this case editor1, is this possible?
You cannot create a stand-alone variable name that way (except as a global) (edit or except with eval()), but you can create an object property:
var tab_counter = 1;
var someObject = {};
someObject['editor' + tab_counter] = "bla bla";
You can create globals as "window" properties like that, but you probably shouldn't because global variables make kittens cry.
(Now, if you're really just indexing by an increasing counter, you might consider just using an array.)
edit also see #Birey's somewhat disturbing but completely correct observation that you can use "eval()" to do what you want.
It is possible
var tab_counter=1;
eval("var editor"+tab_counter+"='blah'")
alert(editor1);
eval("var editor"+tab_counter+1+";")
editor2='blahblah';
alert(editor2);
http://jsfiddle.net/5ZLYe/
You can do the eval method used by Birey or you can create a custom property of an object such as...
obj[editor + tab_counter] = blabla;
But it sounds like you're going about doing whatever you're doing in a particularly horrible way. If you just want to store multiple items which you can index into use an array...
var array = [];
array[0] = blabla;
array[1] = blabla2;
alert(array[0]); //shows value of blabla
alert(array[1]); //shows value of blabla2
It seems like you may want to consider using a Dictionary for something like this. This link which references this link describes your options there.