How to use a dynamic value for object's field? [duplicate] - javascript

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

Related

jquery object get value by known key [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 12 months ago.
Object { "07.03.2022": "106", "08.03.2022": "114" }
i want to get the value (e.g. 114) from the known date 08.03.2022
I've already managed to do it the other way around:
const keyx = Object.keys(response.activeDates)[Object.values(response.activeDates).indexOf('114')];
but how can i get the 114 as result from the known 08.03.2022 ?
this donĀ“t work:
var xto = "08.03.2022";
console.log(response.activeDates.xto);
Thanks!
Did you try like this:
console.log(response.activeDates[xto])
When you are using xto it will look for the key named xto and not the value of that variable.

How to replace .Id with a variable string value [duplicate]

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

Get values of key in object dynamicaly [duplicate]

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.

Use a variable to a variable [duplicate]

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]

get value with string key in javascript [duplicate]

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?

Categories

Resources