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

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'];

Related

get Object value using Object's key where key is a string [duplicate]

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 last month.
I have one object
my_object = {"first_name":"Harry", "age":"24", "second_name":"snow"};
I can get the first name value using
console.log(my_object.first_name); //Harry
and I am getting Harry . Here everything working fine . But for the below code I am not getting values
var num_1 = "first_name";
console.log(my_object.num_1); //undefined
console.log(my_object+"."+num_1); //[object Object].first_name
Please help to solve this. Why I am not able to get object values using num_1 . I need to get the object values using num_1.

how to access property with hyphen in name in JS JSON [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 2 years ago.
Im trying to access an element in JSON With Hyphen in the property name
id("main").textContent = jsonData.res.main-result;
but that is causing an error
const json = {
'property-name': 'something'
}
console.log(json['property-name']);
Use jsonData.res['main-result']

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.

Cannot read properties of a JavsScript function [duplicate]

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]);

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

Categories

Resources