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);
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 4 months ago.
I am trying to add a value in place of json object key but it always returns variable name.
My Code:
var projectName='';
let tempArray=[];
let output={};
for(i=0;i<myJsonArray.length;i++){
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output= {projectName :tempArray};
console.log(JSON.stringify(output));
This returns a JSON as
{"projectName":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
But I need something like this:
{"ABC":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
Can someone help on what I am missing here.
Kind Regards.
You should wrap the project name into [] that would help to make a value become a key
var name = '';
let tempArray = [];
let output = {};
for (i = 0; i < myJsonArray.length; i++) {
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output = {
[name]: tempArray
};
console.log(JSON.stringify(output));
P/s: I don't see any projectName variable there, so I replace it by name instead.
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.
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;
}
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);
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);