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']
Related
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 2 years ago.
In JavaScript everything is object like String literal, numbers, Object...
And properties can be added to objects dynamically also, but I am adding property to object which refer to string then it seems it is not working
Following is JS code which I am running
var aVar = "some string";
console.log(aVar);
aVar.name = 'raj';
console.log(aVar.name);
Output...
hi
some string
undefined
This question already has answers here:
Should I use an empty property key?
(5 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
Javascript objects can accept an empty key with some value but how do we access the value of that empty key ?
var obj = {
"": "Name"
}
Try the following:
var obj = {"":"abc"};
console.log(obj[""]);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Access JSON or JS property using string
(4 answers)
Closed 6 years ago.
I can do var foobar = foo.barto access the bar variable.
How would I do this if I had an arbitrary string that told me what element I needed to access. foo."bar" doesn't work.
I think
foo["bar"]
is the syntax you need
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
i'm working with titanium and alloy.
I need run a JavaScript sentence like
$.D7F15.visible="true";
but D7F15 must be a variable, like
var mifranja="D7F15";
How i can run this??
some help please??
You can use bracket notation to access object property
var mifranja="D7F15";
$[mifranja].visible="true";
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'];