How to get object property with counter [duplicate] - javascript

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.

Related

Unable to bind javascript variable to JSON object key [duplicate]

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.

JavaScript: How to change property of variables stored in an array [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Using Variable for Property Name of Object - Javascript [duplicate]
(2 answers)
Closed 4 years ago.
I have an array that contains variables that are used to control a template.
divisionsListToHide: ['showActivitiesList',
'showAssignActionplanDiv',
'showPropertiesListDiv',
'showAddActivityDiv',
'showCurrentActivity',
'editCurrentActivity'
],
I want to call a method that will set all variables contained in the divisionsListToHide array to false, i.e.
this.showAssignActionplanDiv = false;
this.showPropertiesListDiv= false;
I am trying to create a method (function) to accomplish this:
hideDivisions: function()
{
for(var i = 0;i<this.divisionsListToHide.length;i++)
{
var xx = 'this.' + this.divisionsListToHide[i]; // e.g. this.showActivitiesList
console.log(xx);
eval(xx) = false;
}
},
I was hoping to accomplish this through eval(), however, it displays "Uncaught ReferenceError: Invalid left-hand side in assignment". Any suggestion, how to accomplish this?
this is an object, and you can access its properties in two ways:
this.propertyName and this["propertyName"]. These two ways are the same, except the second one can be used with a variable just like in your case:
var prop = "propertyName";
this[prop] = false; // is equal to this["propertyName] = false, which is equal to this.propertyName = false
So try this:
hideDivisions: function()
{
for (var i = 0; I <this.divisionsListToHide.length; i++)
{
this[this.divisionsListToHide[i]] = false;
}
}

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);

How to dynamically generate variables in JavaScript? [duplicate]

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);

Categories

Resources