Use string as object name in Javascript [duplicate] - javascript

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]

Related

How to use an object's value as the parameter for another object 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 6 years ago.
If I have:
var key = "Section";
course.key = "101";
I get told course.key is unidentified. I'm trying to set course.Section = "101". Is there anyway to pass key's value in the second line?
The property key of course is undefined. Use bracket notation.
course[key] = "101"
is the same as
course["Section"] = "101"

Using the value of a string variable as object parameter accessor [duplicate]

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;

Accessing object in javascript [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
Please explain, why this code it's not allowed in javascript and how to make it.
var p = "inputText";
regError.p
This will give me undefined but
regError.inputText
will give me a correct result.
You can do it by using bracket notation:
regError[p]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
If you have an object like this
var regError = {
inputText : 'something'
}
and you want to access it with a variable, you'll have to use bracket notation
var p = "inputText";
var result = regError[p]; // returns "something"
Use with bracket notation:
regError[p]
You can check the difference between them here and there

What does window['someKeyword'] mean in JavaScript? [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
What does window['someKeyword'] mean in JavaScript?
I guess the window is the DOM window object, but the syntax to retrieve a property is window.propertyName in the reference I found, not window['propertyName']. Are these two ways to retrieve the property?
To access a field of an object in Javascript there are two ways.
let's this is an object
obj = {name:'Object1'};
You can access name field by two ways
obj.name // returns 'Object1'
obj['name'] //returns 'Object1'
besically [] this method is useful when you have to access a field using a variable like bellow
var obj = {name:'Object1'}
var field = 'name'
obj[field] //returns 'Object1' because it will put value of field in `[]`

Getting the value of an object's member through a variable [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
dynamically select a javascript object attribute
(1 answer)
Closed 8 years ago.
I have a javascript object (named resource) used for translating my texts.
resource
.fileNotFound >> "File not found"
.advSearch >> "Advanced search"
Usually I use this like below:
alert (resource.advSearch);
Now I need to access one of the member of this resource object through a variable.
Example:
var trans = "advSearch";
My question: how can I get the translation in my resource object for 'advSearch' contained in the trans variable?
Thanks.
You need to use the bracket notation instead of dot notation as the member operator
resource[trans]
You can use the [] notation to access properties as well.
resource[trans];

Categories