How to dynamically generate variables in JavaScript? [duplicate] - javascript

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Use variable for property name in JavaScript literal?
(3 answers)
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 8 years ago.
My goal is to dynamically generate variables foo1, foo2 and foo3 and assign bar to them using the following code:
for (var i=1;i<=3;i++) {
myapp.set({ foo+i: "bar" })
}
I tried using eval on foo by it doesn't work. Any ideas?

for (var i=1;i<=3;i++) {
var myObj = {};
myObj['foo' + i] = 'bar';
myapp.set(myObj);
}

You can do this with square brackets. If you want the variables to be in the global scope, then use window['foo'+i].
Eg:
for (var i=1; i<=3; i++) {
window['foo'+i] = 'bar';
// OR, if you want them in 'myApp' scope:
myApp['foo'+i] = 'bar';
}

http://jsfiddle.net/TASfG/
var myApp = {};
for (var i=1; i <= 3; i++) {
myApp['foo'+i] = "bar";
}
console.log(myApp);

Related

How to get object property with counter [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 3 years ago.
I am reciving the formValues object, and when I console.log it, I get this result:
attribute_0: "2478"
attribute_1: "2475"
attribute_2: "2480"
What I need is to take those values and store it in a hidden input field, to have something like this:
<input type="hidden" value="2478,2475,2480">
And I could do it, but somehow I can't access this properties in a for loop, so not to do it like:
formValues.attribute_0;
formValues.attribute_1;
formValues.attribute_2;
Because it will be a lot of this attributes, so I need something like:
var attributeValues = '';
for (var i = 0; i < 3; i++) {
attributeValues += formValues.attribute_{i};
}
Or
var attributeValues = '';
for (var i = 0; i < 3; i++) {
var text = 'attribute' + i;
attributeValues += formValues.text;
}
But no. Undefined is what I get :(.If anyone knows how to deal with this kind of stuff, help will be most appreciated.

declaring object using variable [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I am trying to use a variable when declaring an object:
var name1 = "object1";
var data1 = 3;
create_object(name1, data1);
function create_object(name, data) {
var x = {
name: data
}
return x
}
I want x to be stored as
var x = {
object1: 3
}
But my function will make
var x = {
name: 3
}
Is there a way to pass a variable when declaring the name of a child inside an object?
Thanks a lot
To specify a name of a property from a variable you need to use the square brackets notation like this:
function create_object(name, data) {
var x = {};
x[name] = data;
return x;
}

can i pass a string to a javascript function and use it as an attribute of an object? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
i want to pass a string to a javascript function and use it as an attribute like this :
function object (){
this.somevalue = 1;
this.somevalue2 = 1;
this.updateAttribute = function(name,value){
this.{name} = value;
}
}
can it be done ? Thank You .
Yes, you can use [] operator bracket notation:
this[name] = value;

use variable on left side of object declaration [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
Is there any option as I can use a variable on the left side of my object declaration? Something like this:
var col = 'col';
var gridDataVK4000 = {
items : []
};
for (var i = 0; i <= 14; i++) {
col += col + i;
// we now push to the item property
gridDataVK4000.items.push({
col : i,
});
}
because my example isn't working. :(
You'll need to declare it outside of the curly braces using square bracket notation, otherwise you're assigning the key "col", literally, to your object.
var result = {};
result[col] = i;
gridDataVK4000.items.push(result);

Create dynamic arrays using for loop with jquery/javascript can not access outside or cant access global [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have need to create arrays dynamically using JavaScript/JQuery.
What I did is as follows:
var count = 5;
for(var j=0;j<count;j++){
var arrayname = "array"+j;
var arrayname = [];
}
After creation I am expecting arrays array0[],array1[],array2[],array3[],array4[]
So I printed as
alert(array0);
But I am getting error as follows:
Uncaught ReferenceError: array0 is not defined
It occurred because array0[] is not global its bound is only inside that for loop. How can I create dynamic array so that all the arrays can be accessed from outside also?
You can use eval() to define variable dynamically
var count = 5;
for (var j = 0; j < count; j++) {
eval('var array' + j + '=[]');
}
console.log(array0);

Categories

Resources