Calling a JSON Value by giving the key as variable? - javascript

I want to call a JSON value by giving the key via a variable.
Given
if (data[airplane].RunwayThreshold !== undefined){}
Can I make RunwayThreshold a variable?

You can chain bracket access:
data[airplane][RunwayThreshold]
although you may want to check for undefined values along the chain.
Because access is left-associative, that becomes:
(data[airplane])[RunwayThreshold]
and allows you to drill down into objects.

Related

Cannot use ${}while accessing a property inside an object

So I'm trying access the property named all like this
let lang = organizationLocales[0].locale;
alertDetails.alertMessage.`${lang}`.all
But sadly, I'm getting an error near that dollar symbol saying that "identifier expected", I just want to access the property in a dynamic way as the variable lang will be changing based on the user input. I'd appreciate any help, thanks in advance.
P.S organizationLocales is an array that has nothing to do with alertDetails object.
You can access to the property of an object through the index accessor []. So if you want to use a string to address a property, you should enclose such string inside square brackets.
You have to think of it like an associative array where each property name is paired to its value, including functions. Since in js syntax, the dot expects the property name statically defined, you cannot compose an identifier like that. But yet you can use a different way to get there without relying on dot.
To better answer to your specific problem, this may be the solution:
let lang = organizationLocales[0].locale;
alertDetails.alertMessage[lang].all

attach string variable to document object

I have a variable called current_slide which contains a string called "default"
Now I have a object called document.referencemap:
How can I attach the "default" string from current_slide to the object document.referencemap so that I will get
document.referncemap.default ?
What is the best way to attach a string to the object?
At the moment I am calling the object property manually like:
document.referncemap.default.
Can anybody give me a hint so I can solve this issue?
My code looks like this
Because the current slide is always changing i need to load the object from the value of current_slide.
You can use this:
temp0 = document.referencemap[current_slide.slide];
Explanation:
There are two ways to get values from Javascript objects,
using property notation: temp0 = document.referencemap.welcome;
using dictionary notation: temp0 = document.referencemap["welcome"];
This will get the exact same item from the object. Note however that 2. uses a string key. Because of that you can also use a variable that contains a string, in this case: temp0 = document.referencemap[current_slide.slide];
use:
document.referencemap[current_slide] = booBar
make sure current_slide is of type string. Read the Property accessors
docs for more information.

Javascript object property logs normally but is undefined?

I have a Javascript object that gets initialised in an asynchronous manner, later edited and then stored in an array until it can be saved. The problem is that one of the checks it goes through is _.has(obj, 'Id'). This test always fails. I've used JSON.stringify() to log the object's internals and everything has its value and looks correct. When I run a simple test of this line with hard-coded values in plunker, everything works as expected. I've tried accessing the Id values through obj.Id and obj['Id'] but these both return undefined and I don't know why. Why can stringify read the property but a regular accessor can't? The Id is a value that is not touched between initialisation and saving of the object.

Why is the property of JSON object not accessible?

I'm not able to access progress property of data object. I used debugger to stop the application and check if data object is defined. Here's the screenshot of the console and it completely makes no sense.
data is an Array. You want data[0].progress.
Data is an array, as evidenced by the fact that in the console it has brackets.
In order to access the object inside it, you need to use array index notation, like so:
data[0].progress //0

Javascript get value from an object with the name contains special charactor

This is my object abcd; This object contains some variables such the names: #promo, #size, #color, #value etc. so how could I print these?
I have use alert(abcd.{"#value"}) but this is showing me an error. So how could I get the value of the variable?
There are two ways to access an object's property: obj.key and obj["key"]. The latter allows you to use any value or variable, which is what you want here.
Therefore, you should use abcd["#value"].

Categories

Resources