This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 9 years ago.
I can't figure out how to get an object property using a string representation of that property's name in javascript. For example, in the following script:
consts = {'key' : 'value'}
var stringKey = 'key';
alert(consts.???);
How would I use stringKey to get the value value to show in the alert?
Use the square bracket notation []
var something = consts[stringKey];
Javascript objects are like simple HashMaps:
var consts = {};
consts['key'] = "value";
if('key' in consts) { // true
alert(consts['key']); // >> value
}
See: How is a JavaScript hash map implemented?
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:
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 property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
var str="firstname";
var obj={};
obj.str="john";
I want to create property firstname but i want to create it by the variable name like obj.str not like obj.firstname here problem is obj.str create property str not firstname.
i want to create property like this because it will later help me to create property by joining two string .
Try like this with [] notation.
See MDN when to use dot(.) or bracket([]) notation for javascript.
var str = "firstname";
var obj = {};
obj[str] = "john";
console.log(obj);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I want to pass a variable into a variable
This is what I want
uk.objects.ARG
I have
var d= ARG
how can I get uk.objects.ARG?
I tried
uk.objects.d
, javascript stores it as the string, not a variable
What should I do in order to make it as a variable ?
I think what you are looking for is perhaps:
var d = "ARG";
uk.objects[d];
You can access JavaScript objects' properties also with the bracket notation:
object[property] = value;
The same works for reading. So in your case you could do the following:
var value = uk.objects[d]
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 an object:
var obj = {fields : []};
And a variable:
var x = "property_name";
Now how do I use the value in the variable x as the property name for the object that I'm going to push inside the array?
I tried something like the one below, but it takes x literally and uses it as the property name. What I want to use is the value stored in the variable x. Is that possible?
obj.fields.push({x : 'im a value'});
You cannot use the object literal syntax for this purpose. However, you can create a new object and then use the [] syntax - remember, obj.xyz is equivalent to obj['xyz'] - but as you can see with the quotes in the latter, you can use an expression there - such as a variable:
var x = "property_name";
var obj = {};
obj[x] = 'value';
obj.fields.push(obj);