How to access a dynamic property: objectName.{variable} [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
jquery dynamic id
(4 answers)
Closed 8 years ago.
i need to access
objectName.myID
but the "myID" part is dynamically generated..
how do i do this?
i tried
this['objectName.'+ variable]
i'd hate to use eval...
ps
this happens in a function (local scope) by the way..

You can access Object properties in two ways:
o.propertyname
//or
o.["propertyname"]
When using the bracket notation you have to put the propertyname in quotes or else it will be interpreted as a variable name (which in your case is exactly what you want). So in your case where you have stored the name of the property as a string, the way to go would be:
var variable = "propertyname";
o[variable];
/* /\ variable is replace with it's string representation "propertyname" */
You can even call methods this way:
var o = {};
var functionname = 'toString';
o[functionname]();
You can mix both notations, your example would look like:
var obj = 'objectName';
var prop = 'myID';
this[obj][prop]
// or this is possible too:
this.objectName[prop]

Assuming propertyName is the name of a variable holding the name of the property, for example 'myId', then you can use.
objectName[propertyName]
More details in the MDN : Working with objects

Related

Firestore Cloud functions use variable in property for update document [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 2 years ago.
Trying to use a variable as property when updating a document via Cloud functions.
Maybe I miss the syntax here or is it simply not possible.
var value = 'myValue'
var name = 'myVariable'
admin.firestore().collection('mycollection').doc('mydocument').update({ name : value});
The document is updated but it shows then name = myValue and would like to see in the document myVariable = myValue.
What would be the syntax to achieve that to be flexible?
Got it.
var value = 'myValue'
var name = 'myVariable'
admin.firestore().collection('mycollection').doc('mydocument').update({ [name] : value})

Use string as object name in Javascript [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Is it possible to access the property of a javascript object using a string?
example: I have a javascript object "obj" that contains a property 'index'.
obj.index = 4;
Now let's say I have a string whose value is the same as a property name of my object:
var str = "index";
Can I use the value of the str variable to access the 'index' property of 'obj'?
Just use the following code:
obj[str]
You can use array Notation (squire bracket notation )
obj[str]

Javascript syntax clear up [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
var me = new Object();
me.name = "Adithya";
me.age = 15;
for(var prop in me){
console.log(me.prop);
}
When I run this, it prints undefined. I don't understand why it doesnt print the value. When I type
console.log(prop);
it prints
name
age
Doesn't that mean that prop is a a field in me? So shouldn't me.prop(when prop is name) , work like me.name ?
prop is a variable containing a string so you need to use me[prop] like this:
for(var prop in me){
console.log(me[prop]);
}
When your property name is in a variable, you need to use the [prop] syntax which in your case would be me[prop].
FYI, me.prop tries to access a property named "prop". It does not use the contents of the variable prop. You can only use the dot syntax to access a property when the property name is a constant that is known at code parse time and when the characters in the property are legal to use in the Javascript dot syntax. If it's in a variable (or if it contains characters like a . or a space), then you must use the bracket syntax which will fetch the contents of the variable and use that property name.
The syntax you're looking for is me[prop];
me.prop is equivalent to me['prop'] which is not what you want.
notice you didn't assign a value to name or age before when you did me.age and me.name.

Adding custom variables to an object [duplicate]

This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
Suppose I have an object obj which has some variables in it. What I want to do is to add a new variable which has a name i gave as a parameter.
For example:
var obj=
{
number:8
}
function AddField(field_name,obj)
{
//some magical code here
}
If I call
AddField('name',obj);
i want to be able to do
obj.name='Apple'
after calling this function.
Any ideas?
Use square brackets notation:
obj[field_name] = value;
REF: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId
Use the bracket notation:
obj[field_name] = void 0;
This creates a new property with an undefined value.
If obj is already defined, you can just do obj.field_name = null or obj["field_name"] = null.
I wouldn't set any value to undefined even though the language allows for that, simply for a semantic issue: undefined means "this property/variable has not been defined" and null means "this property/variable has no value".

How to refer to object fields with a variable? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
Let's asume that I have an object:
var obj = {"A":"a", "B":"b", "x":"y", "a":"b"}
When I want to refer to "A" I just write obj.A
How to do it when I have key in a variable, i.e.:
var key = "A";
Is there any function that returns a value or null (if key isn't in the object)?
Use bracket notation, like this:
var key = "A";
var value = json[key];
In JavaScript these two are equivalent:
object.Property
object["Property"];
And just to be clear, this isn't JSON specific, JSON is just a specific subset of object notation...this works on any JavaScript object. The result will be undefined if it's not in the object, you can try all of this here.
How about:
json[key]
Try:
json.hasOwnProperty(key)
for the second part of your question (see Checking if a key exists in a JavaScript object?)

Categories