Get values of key in object dynamicaly [duplicate] - javascript

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.

Related

get Object value using Object's key where key is a string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed last month.
I have one object
my_object = {"first_name":"Harry", "age":"24", "second_name":"snow"};
I can get the first name value using
console.log(my_object.first_name); //Harry
and I am getting Harry . Here everything working fine . But for the below code I am not getting values
var num_1 = "first_name";
console.log(my_object.num_1); //undefined
console.log(my_object+"."+num_1); //[object Object].first_name
Please help to solve this. Why I am not able to get object values using num_1 . I need to get the object values using num_1.

How to return value to the parent form [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have a popup form and need to return a value to the master form. The first statement works fine, but when I try to put the field name (textAlert) to a variable, I cannot get it to work. I tried all kinds of syntax.
Please help. Thanks.
function DoReturnValue()
{
opener.MasterForm.textAlert.value = "Hello"; // WORKS
/* DOES NOT WORK
var theField = "textAlert";
opener.MasterForm(theField).value = "Haloha";
opener.document.MasterForm.getElementById(theField).value = "Haloha";
*/
}
To access properties dynamically with a string variable as a name, use square-bracket notation:
opener.MasterForm[theField].value = "Haloha";

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?

Dynamically assign name to object array [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 10 years ago.
I have a javascript issue.
If I have an object array objAr, the object consists of id,name.
If I was to access objAr[0].id it returns the id value of the first object. What would happen if the object is dynamic and therefore I do not know what it consists of, is there a way to dynamically call the Object attribute?
Currently I am creating another array
var theArr = new Array("id", "name");
and call:
objAr[0].theArr[0] instead of objAr[0].id.
Is there a way to do this better using Javascript?
With Javascript you can call all of the attributes in an object without knowing the keys.
See below:
for(key in objAr[0]) {
console.log(objAr[0][key]);
}
If you just wanted the first attribute you could run:
for(key in objAr[0]) {
var attFirst = objAr[0][key];
break;
}
Additionally for the JS array you could have used square brackets.
var theArr = ["id", "name"];
hope that helps
In javascript you can always use the "array notation" in place of the "dot notation"
So these 2 lines are the same
objAr[0].id
objAr[0]["id"]

Categories

Resources