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
Related
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.
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.
This question already has answers here:
Javascript Object - Key beginning with number, allowed?
(4 answers)
Closed 6 years ago.
I have a php array that I'm json_encodeing into a JavaScript object. When I preview the object in console, it looks something like this:
Object { 1="some text", 2="something else", 3="extra text"}
Shouldn't I be able to read the value for index 1 like this (pretending my object name is obj)?
obj.1
Doing that gives me the undefined error message. How would I access the value for the exact index of 1?
You can only access a property using dot notation if the property name is a valid identifier. Identifiers cannot start with a number.
You have to use square bracket notation for other properties.
obj[1]
1- Do not use the word Object
2- Use colon instead of equal
3- Do not forget the semi-colon
4- if your key is number, use braces.
var obj = { 1:"some text", 2:"something else", 3:"extra text"};
console.log(obj[1]);
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'];
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