jquery object get value by known key [duplicate] - javascript

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.

Related

How to use variable to dynamically change object attribute? [duplicate]

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

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

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

How do I get the Value of a specified Key in JavaScript? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I know this question has been asked a million times, I just can't seem to make the answers I find fix my problem.
All I'm trying to do is return the Value of the key that is specified. They Key is in the variable "t":
RefDataTables.forEach(function(t, ti) {
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].t);
});
I know the ".t" is wrong because that is looking for a property (I'm not sure the right word to call it) named "t"
Since "t = "HandleType"" What I'm trying to accomplish is the equivalent of:
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].HandleType)
You can access it by passing the variable between the square brackets.
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0][t])

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