This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Closed 9 years ago.
I have a couple of check boxes that I'd like to use their "name" attribute as the value I reference within my array.
Let's say I have an array like so:
var test = {
"one" : 1,
"two" : 2,
}
var someBtn = "one";
How can I say the following?
test.someBtn; // Which would translate to test.one
Obviously, test.someBtn does not work. Is this even possible?
Just refer to it as if it were an array:
test[someBtn]
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
I currently have the following in my Javascript code where Id is a field of an object:
this.recId = event.detail.row.Id;
I want to make the Id part generic something like
String a = 'Id';
this.recId = event.detail.row.a;
How do I achieve this in Javascipt
Use [ ] instead.
const obj = {
id: "myObject"
}
const a = "id"
console.log(obj[a]);
In your example, it would be
this.recId = event.detail.row[a];
You can use
this.recId = event.detail.row[a] to get the desired result
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
let varone = "vartwo";
let vartwo = "answer";
And I would like to get "answer". But I cannot do the following:
console.log(vartwo);
How can I get "answer", but not by including "vartwo" in my code?
I think one way to resolve it, is by using javascript objects, as shown:
obj={
varone:"vartwo",
vartwo:"answer"
};
console.log(obj[varone]) //outputs "answer"
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I have an object SentimentScore as such
SentimentScore:
{Mixed: 0.00005830301233800128,
Negative: 0.0011920471442863345,
Neutral: 0.9854754209518433,
Positive: 0.013274261727929115}
I want the values based on what I have set in a variable, for eg:
var finalSentiment1 = 'Neutral';
var finalSentiment2 = 'Positive';
I want to use the variables instead, to find the values of fields of the SentimentScore.
Something like, SentimentScore.finalSentiment1 should give 0.0011920471442863345.
Is it possible without using a conditional statement? If yes, how? Thanks in advance.
You can use computed ([]) property which will allow you to have an expression to be computed as a property name on an object dynamically:
var SentimentScore = {
Mixed: 0.00005830301233800128,
Negative: 0.0011920471442863345,
Neutral: 0.9854754209518433,
Positive: 0.013274261727929115
}
var finalSentiment1 = 'Neutral';
var finalSentiment2 = 'Positive';
console.log(SentimentScore[finalSentiment1]);
console.log(SentimentScore[finalSentiment2]);
This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Closed 6 years ago.
Is it possible to use button attribute as a variable name of an array in JavaScript? I tried to do it as follows but it returns syntax error.
var $(this).attr("value") = [];
Is there any way to do this?
var buttonValueAttribute = $(this).attr('value');
This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Convert string to variable name in JavaScript
(11 answers)
Closed 7 years ago.
I know there are other questions similar to this, but I feel as if they don't answer my question. I would basically like to make Dynamic Variable Names. Let me give you an example:
//Take the strings "new" and "variable" and make them into a variable.
"new" + "variable" = "Hello World";
//So technically the variable name is "newvariable" and has the value "Hello World"
So basically it takes two strings, and combines them into one variable name. How would I go about doing this?
P.S. This is NOT to combine the values of variables, just the names
Write it into an array or object
var arr = {};
arr['new' + 'variable'] = 'Hello World';
then
alert(arr['newvariable']);
or
alert(arr.newvariable);