How to add number with variable name? [duplicate] - javascript

This question already has answers here:
How do I create dynamic variable names inside a loop?
(8 answers)
Closed 3 years ago.
Hi I want to change the variable name like stop1, stop2, stop3 etc in a loop.
I tried using for loop with stop + i but it didnt work
Please help

var varMap = {};
for(var i=1; i<10; i++) varMap['stop'+i] = 123;
I think this will help

Related

Local variable not being recognized inside for loop - Javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
I'm trying to reference a variable inside this for loop, but its not working. It recognizes the first variable in the line but anything beyond that it won't recognize
var farmsArray = Object.keys(farmsPath);
var farmsList = new MessageEmbed()
.setTitle("Available Farms")
.setDescription("Where would you like to plant?")
.setColor(0xFFF00);
for(i=0; farmsArray[i] != undefined; i++){
console.log(i);
farmsList.addField(` ${farmsPath.farmsArray[i].name}`, farmsPath.farmsArray[i].size);
it recognizes this^ ^ but not this
};
Can someone help me find out why? I've been trying for a while now and I can't seem to figure out the issue.
Based on var farmsArray = Object.keys(farmsPath); I'll imagine farmsPath is an object whose keys you want to iterate over.
var farmsList = new MessageEmbed()
.setTitle("Available Farms")
.setDescription("Where would you like to plant?")
.setColor(0xfff00);
Object.keys(farmsPath).forEach((key) => {
farmsList.addField(
` ${farmsPath[key].name}`,
farmsPath[key].size,
);
});
could be closer to what you're looking for.

How can I make new variables out of elements in a list? [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 3 years ago.
I have a list of 3 lines. I want to efficiently separate each line out as it’s own individual item.
myList = [MultiLine.attributes.renderers[0], MultiLine.attributes.renderers[1],
MultiLine.attributes.renderers[2]]
What I want:
line1 = MultiLine.attributes.renderers[0];
line2 = MultiLine.attributes.renderers[1];
line3 = MultiLine.attributes.renderers[2];
I know in Python there is the casting function which I would use like so:
line + str(i) = MultiLine.attributes.renderers[i];
However I'm getting ReferenceError: invalid assignment left-hand side with the equivalent JS:
for(var j = 0; j < myList.length; j++){
"line" + String(j) = MultiLine.attributes.renderers[j];
}
Any ideas?
I don't know how to properly go about changing the value of my variable within the for-loop. Would Break statements help?
You could use a destructuring assignment with the array.
var [line1, line2, line3] = MultiLine.attributes.renderers;

i is not incremented within JavaScript for loop [duplicate]

This question already has answers here:
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Javascript: Why do I need to declare var i = 0 in the for loop?
(4 answers)
Closed 4 years ago.
for (i = 1; i < this.people.length; i++) {
peoplePicks[i] = this.people[i].chooseAction(peopleChoices[i]);
}
I have this for loop within my JavaScript program. It runs for ever, even though the length of the array that I am passing to it is 2. when I print the value of i after the statement of the for loop, I get 0. So it seems like i is being decremented by executing the for loop's statement. How can I fix this?
Add var before your i variable in the initialising of your for loop.
Like this for (var i = 1; i < this.people.length; i++) {

Tricking Javascript variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

Updating a result in JSON with a dynamic variable name [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to update a value in my JSON.
My JSON result looks like this:
results = {"ROWCOUNT":50,"COLUMNS":["PERSONID","NAME"],"DATA":{"PERSONID":["42","43","44"], "NAME":["JOE","TOM","JANE"]}
resultData = results.DATA
In the below code I am looping over the result set and attempting to update a value at a position. I believe it is failing because I am not using dynamic variables correctly.
var columnName = "NAME";
for(i=0; i < results.ROWCOUNT; i++ ){
resultData.columnName[i] = "foo" // failing here due to "columnName" being dynamic.
}
Figured it out.. You have to use array syntax
resultData[columName][i]

Categories