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".
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I'm creating a mongoose schema and need to do required checks a lot, so I tried creating this function. But it says that 'subcategory' is initialized but never used. How can I get the function to check this. like I need it to?
function requiredCheck(subcategory, value) {
return this.subcategory === value;
}
Use bracket accessors to check a property of this by variable:
function requiredCheck(subcategory, value) {
return this[subcategory] === value;
}
Otherwise you're checking for the property with the literal key "subcategory" which probably isn't what you want.
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.
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I am attempting to dynamically access an object with a property value that is contained within a string. Example below:
var toolState = {
draw_point: false;
draw_line: false;
}
var dynamicText = "draw_point";
toolState.dynamicText = true; //here is the problem
I'm fairly new to JS. Sorry if this is a silly question.
Thanks
Use bracket notation instead of dot notation for variable names as properties.
toolState[dynamicText] = true;
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
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?)