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]
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
let property = $(this).val();
let property_name = this.dataset.property;
headObject[selected].property_name = property;
});
property_name is a variable not key. Are there anyway to handle this? Thank you in advance!
I can see you are uing jQuery.
headObject[selected].attr( property_name, property ); //Where headObject[selected] is a jQuery object
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:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I get the values of the first or NAMED key in the object. This one is called Efficacy.
var PSEQ_Obj = $.parseJSON( PSEQ );
var PSEQ_dps1 = PSEQ_Obj.Efficacy;
But I want to know if there is a dynamic way to do this.
For instance. Bellow will return Efficacy.
for (label in PSEQ_Obj) break;
But I cant use it lik follows
var PSEQ_dps1 = PSEQ_Obj.label;
So this label is a dynamic, but you cant call it exactly like above.
Is there a different way?
you have to use bracket notation instead of dot notation when accessing properties that are stored in variables as follows :
var PSEQ_dps1 = PSEQ_Obj[label];
you can read more about this topic on MDN.
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?
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
How to access an object using a variable as key. Here is my code sample:
var o = {"k1": "111", "k2": "222"};
alert(o.k1); //working fine
var key = "k"+1; alert(key); // k1
alert(o.key); //not working
You can access objects like arrays:
alert(o[key]);
Change the last line to: alert(o['k1']); or alert(o[key]); where key is your dynamically constructed property key.
Remember you can access object's properties with array notation.
Consider using a for...in loop