Dynamic variable in loop javascript - javascript

I want to create a dynamic variable in the loop.
I found something about eval and window but I don't know how to use this.
This is my loop and i want to create a 9 variables names from m1 to m9. I mean that the name of variable must be m1 to m9
for(i=1; i<10; i++){
var m+i = "Something"
}
Please help me with this. Really appreciate.

You don't want to create 9 variables. Trust me. You want to create an object.
var m = {};
for(var i=1; i<10; i++){
m[i] = "Something";
}
You can also create an array (m = []), but since you are starting at 1 and not 0, I'd suggest an object.

But if you still want to create 9 variables, despite all that, you still can:
for(i=1; i<10; i++){
eval('var m'+i+'='+i)
}
(And yes, you shouldn't).

var object = {};
var name = "m";
for(i=1; i<10; i++){
object[name+i] = "Something";
}
console.log(object.m1); // "Something", same for m2,m3,m4,m5...,m9
However consider if the "m" is really necessary, arrays are way faster:
var array = [];
for(i=1; i<10; i++){
array.push("Something");
}
console.log(array[0]); // "Something", same for 1,2,...,8

Related

Declare and fill in the an array with the for loop

I want to create an array using a for loop in JavaScript. I want my array to be consisted of 10 variables or more (var kaunt1, var kaunt2, etc...) which will be actually numbers from div tags.
I tried this code below, but it isn't working??? Am I missing something?
var arr = [];
for(var i=1; i<=10; i++) {
var kaunt[i] = parseInt(document.getElementById("A"+i).innerHTML, 10);
}
var kaunt[i] = ... isn't how you add an index to an array, that's a syntax error.
Just use kaunt[i] = ....
You're declaring arr, but using kaunt? Not sure what that's about, but you should normalize that if they're meant to be the same thing.
Anyway, use kaunt.push(parseInt(document.getElementById("A"+i).innerHTML, 10)); (no var) inside your for.
Other's beat me I think, but this should do it...
var kaunt = new Array();
for(var i=1; i<=2; i++) {
kaunt[i] = parseInt(document.getElementById("A"+i).innerHTML, 10);
}
Get rid of the var in front of kaunt[i].
kaunt[i] = ....

How to iterate over JSON data

The following is my JSON data in a div:
[{"Identifier":"1","Label":"10 Day","Categories":"Standard","UpdatedBy":"Lno","UpdatedAt":"01-02-2013","RefId":"0","ComType":"1","Cs":["944"],"AM":"Error Message","Message":"asdfasdf","Combiner":[{"uniqueID":"1","type":"7","rule":""}]}]
I am accessing it through a JS object:
var myArrayVar=JSON.parse(document.getElementById("populateDT").innerHTML);
I want to iterate over this JS object. The following is my code, but it doesn't access my key/value fields:
for(var i=0; i<=myArrayVar.length;i++){
for(var j=0; j<=myArrayVar.Combiner.length; j++){
var sessionUniqueId= myArrayVar.Combiner[j].uniqueID;
alert(sessionUniqueId);
var sessionType=myArrayVar.Combiner[j].type;
alert(sessionType);
var sessionRule=myArrayVar.Combiner[j].rule;
alert(sessionRule);
}
}
Can anyone suggest a solution?
for (var i = 0; i < myArrayVar.length; i++) {
for (var j = 0; j < myArrayVar[i].Combiner.length; j++) {
var sessionUniqueId = myArrayVar[i].Combiner[j].uniqueID;
alert(sessionUniqueId);
var sessionType = myArrayVar[i].Combiner[j].type;
alert(sessionType);
var sessionRule = myArrayVar[i].Combiner[j].rule;
alert(sessionRule);
}
}
You never use i. You need it to access the current array element, for example:
for(var j=0; j<=myArrayVar[i].Combiner.length; j++){
myArrayVar is your array, myArrayVar[i] is the i-th element in that array and myArrayVar[i].Combiner is the combiner (array) property of the i-th element.
You'll make it yourself a lot easier if you give the current element a name as well. (You probably want to come up with a less generic name such as current though.)
for(var i=0; i<myArrayVar.length;i++){
var current=myArrayVar[i];
for(var j=0; j<current.Combiner.length; j++){
var sessionUniqueId=current.Combiner[j].uniqueID;
alert(sessionUniqueId);
var sessionType=current.Combiner[j].type;
alert(sessionType);
var sessionRule=current.Combiner[j].rule;
alert(sessionRule);
}
}
Also, i cannot equal myArrayVar.length as that index is already out of bounds. Your loop condition should have < instead of <=.
You have an array with one element. That element is in myArrayVar[0] and it is an object. To iterate over the object use a for ... in loop.
var myObj = myArrayVar[0];
for(var key in myObj){
var value = myObj[key];
console.log(key, value);
}
You should also use console.log for debugging. It will show you more information about objects than alert can.
Try using "<" instead of "<=" in the for loops, and "myArrayVar[i].Combiner" instead of "myArrayVar.Combiner".
There are a couple of problems I see. First, your i and j variables go one spot too far. They should be using < instead of <=.
Secondly, you're declaring variables inside the loop. That's fine, but JavaScript isn't block scoped, so you really end up with the same three variables overwriting each other as many times as there are items in the list. Your example data only has one item so you probably won't notice the overwriting problem just yet–but once you have multiple items in the list it could be a problem.

how to add properly the value of counter to a variable name

it can seems easy, but I'm little lost...
What I need is to add the counter value to a variable name.
for (var i=0; i<8; i++){
var upBt0; //this is the name in the first iteration
var upBt1; //this is the name in the second iteration
.
.
.
var upBt8; //this is the name in the last iteration
}
How can I do this properly?
Sorry, Daniel
EDIT:
for (var i=0; i<8; i++)
{
this.upBt = "upBt"+i;
this.upBt = new PL_Button().init("upBarButton"+i);
}
I create buttons...in particular 8 buttons...
And later, I need access to each of these buttons:
function (){
this.upBt1;
this.upBt1;
this.upBt3;
this.upBt6;
}
Hope have explained better.
EDIT 2:
Finally, I solved it using an auxiliar array of the class, where I pushed each object in each iteration. Each item of this array is a real reference to each object, so changed the item in the array, changes are also made in the corresponding object...
Hope have explained well.
Thanks for your help,
Daniel
You can use this, but it's ugly code...
for (var i=0; i<8; i++){
this["upBt" + i] = Object.create(null);
}
Use an array helped to me:
this._arrayButtons = {};
for (var i=0; i<8; i++)
{
this.upBt = new PL_Button().init("upBarButton"+i);
this.upBt.imgPath = "res/builder/"+upBarImgs[i];
.
.
.
this._arrayButtons[i] = this.upBt;
}
After, in one function, I can access to the content of the variables such as:
function refreshFrameAlpha(){
this._arrayButtons[3].alpha = 125;
this._arrayButtons[5].alpha = 225;
.
.
}
In this way, I can refresh alpha (p.e) of the object, because each item of the array is a reference to the corresponding object.

How do you combine a form element with a variable?

First, how do you append data to the end of a variable in Javascript? I know for PHP you use:
$foo = "bar";
$foo.= "bar2";
Next, I can get the following to work for one form field:
var test = document.form1.option1.value;
However, how would I go about doing this with a for loop whilst appending each iteration onto the end of the variable? For example (where XXX is the loop variable i):
for(i=0; i<10; i++){
test.= document.form1.optionXXX.value;
}
Basically, I need all the data from a random set of form fields that can range anywhere from option1 to option20. Thanks.
Try using this notation to avoid eval().
for(i=0; i<10; i++){
test += document.form1['option' + i].value;
}
var test = "";
for (var i=0; i<10; i++){
test += document.form1["option" + i].value;
}

Building Array in forloop

I'm trying to create a basic for loop which creates an array but am a little confused in how to structure it... this is an example of the array:
var list = [
{id: "135", data: [9,129,345, 687]},
{id: "239", data: [596,382,0,687,33467]}
];
Those are just example numbers but i need to make it in a for loop (because the numbers come from variables, but i've got no idea how i do it =/
I know how to make a 1 dimensional array with a for loop but not anything this complex..
Does any one have an example loop to show how its structured?
EDIT: Just understood what you meant.
This is a bit of pseudo to show how you would create an array like the one in question
var list = [];
for(var i = 0; i < objectcount; i++){
// Here you need some way to get the ID for each should be dependant on 'i'
var obj = {id: 10, data: []}; object
for(var j = 0; j < datacount; j++){
// You need some way to retrieve each data number, dependent on 'j'
obj.data.push(somenumber)
}
list.push(obj)
}
OLD ANSWER
Assuming that you want to create an array of all the data arrays inside each object try something like this:
var newarray = [];
// This will iterate through each object in the list array.
for(var i = 0; i < list.length; i++){
// This will iterate though each value in the data array
// of the current list object
for(var j = 0; j < list[i].data.length; j++){
// Then you add that value to 'newarray'
newarray.push(list[i].data[j]);
}
}
Not sure if this is what you're asking, but to access the list object as shown you could
alert(list[0].id);
alert(list[0].data[0]);
and you would get 135 and 9 respectively.
Here's the for loops to see them all:
for (i=0; i<list.length; i++) {
alert (list[i].id);
for (j=0; j<list[i].data.length; j++) {
alert (list[i].data[j]);
}
}

Categories