Returning Value of property in Javascript [duplicate] - javascript

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 3 months ago.
Below is a simple Object I created in Javascript.
var obb = {name : "Charlie", Age : 28 , Location : "London" , Job : "Detective"} ;
var x = "name";
console.log(obb.name);
console.log(obb.x) ; //Dot notation :- returns value undefined
console.log(obb[x]); // Square bracket :- returns correct answer
I know that there are two methods to fetch values of objects i.e dot notation and square bracket.Now if I am storing value of property in a variable & using dot notation with the variable to fetch value , why is it not working ?

In JavaScript, the object attributes can be accessed using either the dot notation or the bracket notation. Dot notation is frequently used because it is simpler to read and understand. What is the significance of bracket notation and why should we use it? The square bracket syntax [] turns the expression within to a string, allowing us to access object properties via variables.

Related

Difference in referencing an object using object.key = value and object['key'] = value [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 6 years ago.
What is the difference between referencing an object with object.key = value and object['key'] = value For example if I had an object call var myObj = {};
I believe the quote notation allows for arbitrary keys, (like console.log({' 1a':3}[' 1a']), whereas the dot syntax can only be used with keys that begin with a letter.

Why I can't access JSON dictionary element using dot notation? [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 6 years ago.
I have a following JSON representation:
var collectionCopy = JSON.parse(JSON.stringify(
{
1 : {
2: "2"
}
}
));
Why cant I access key "2" using dot notation (i.e. collectionCopy.1.2) ?
You can use the dot notation for accessing an object's properties only on a valid identifiers in the language.
And since numbers (or anything that starts with a number) are not a valid identifiers you can access it (as a property of an object) only with the bracket notation.
This is because the keys are strings not actual numbers:
to access it use:
collectionCopy[1][2]
or
collectionCopy['1']['2']
Relevant docs on accessing properties

how to use . point into javascript variable name [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
I have and API that return an Array of object like the following:
{
"form_submit.form_submit_id": 7987,
"form_submit.exported": false,
"form_submit.updatedAt": "2016-01-18T16:13:16.813Z",
"form_submit.user.user_id": 14,
"form_submit.user.name": "Hugo Bismarck",
},
If I use the . on my javascript for example to get the first element
var test = array[0].form_submit.form_submit_id;
get the following error
array[0].form_submit.form_submit_id is undefined
the problem is that the name of the attributes have points, so how can acces to this attributes?
You can use in this case the bracket notation:
property_name is a string. The string does not have to be a valid identifier; it can have any value, e.g. "1foo", "!bar!", or even " " (a space).
var test = array[0]['form_submit.form_submit_id'];

what is a difference myObj.a=b vs myObj[a]=b [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I was reading here on stack overflow that these are not equal. So what is the difference.
What happened is that in 2nd case value was assigned as the property of myObj. So if b='abc';
then myObj.abc was now available.
I had always thought same thing but [] version was used when name were weird ones.
Dot notation takes an identifier that is the property name. The square bracket notation accepts a string representation of the property name.
Given var a = "a"; then myObj.a = b and myObj[a] = b and myObj["a"] = b are equivalent.
The difference between myObj.a=b and myObj[a]=b is that in the first case you are accessing an attribute called a in the object. In the second you are accessing an attribute whose name is in a variable called a.
On the other hand, myObj.a=b and myObj["a"]=b would be equivalent.
a lot, results would depends on a var value. but ["a"] would be the same as .a

How to use a variable value as a property of an object? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create an object property from a variable value in JavaScript?
I want to create an object using a value in a variable as the property name.
I have a variable called propertyName:
propertyName = "first";
How do I use the value stored in this variable as object property, as in the following?
obj.first = something; // 'first' should be extracted from propertyName
Use square bracket notation:
obj[propertyName] = something;
This should work:
object[ propertyName ];
It's an alternative form to the dot notation. What sets it apart from it is that it allows you to dynamically generate the property name using strings.

Categories

Resources