Accessing Object and Property via Variable? [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I have a namespace setup like
mySpace.util = {
valid: {
first: function() {
//blah
}
}
}
I am trying to pass the valid and first as variables to access the function ? I've tried:
mySpace.util.[variable1[variable2]()] but that doesn't work ? I am trying to pass variables so I get mySpace.util.valid.first()
Any ideas how I could do this?

var obj = 'valid',
meth = 'first';
mySpace.util[obj][meth]();

Related

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

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 access to object properties by string with another object [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I need to access to object properties by string with another object
In php you can reach that like if array $b = 't';$a->$b; is the same as $a->t
var obj = {
title : 'spider'
}
var hero = {
spider : 'is a hero'
}
console.log( hero.spider );//works fine
console.log( hero.obj.title );//i need the same result, from variable (obj)
how can i do that
console.log(hero[obj.title])
I think this should work.

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]

Categories

Resources