This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?
This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 4 years ago.
I have to create a string in format like below
"currentState[0]['children'][1]"
But I need to execute it later just like below
currentState[0]['children'][1]
I have elements and childrens on currentState. But while looping I have to create a string. But later I need to execute as array.
I have tried almost all array methods. Array.call, bind etc. And string methods as well. Could not get the output
How can I make it
Please be more specific with your question but from my understanding, you can use javascript's eval() function to execute a string as javascript, so when you need to execute it, just run eval("currentState[0]['children'][1]").
The alternative to the eval() would be
function evalFn(obj) {
return Function('"use strict";return (' + obj + ')')();
}
evalFn("currentState[0]['children'][1]")
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval for a more in-depth explanation.
This question already has answers here:
What is the $$ (double dollar sign) used for in angular?
(2 answers)
Closed 5 years ago.
I find this code in a AngularJS base front-end application:
var xyz= {
FileUrl: "some url value",
CreatedDate: new Date(),
$$file: "some value in base64 format for file"
};
I search through net but I, do not find any good answer why $$ use for defining object property of JavaScript. I mean what is benefit of using $$ in object property.
$$ means that it's a private variable.
Edit:
This is just a naming convention used by Angular to signal you that you shouldn't use this property directly, as they could remove it or change the usage in a future release.
Since it's just a naming convention, nothing in Javascript prevents you to use it though, however you shouldn't really do it for the reason mentioned above.
This question already has answers here:
Unable to access JSON property with "-" dash [duplicate]
(5 answers)
How can I access object properties containing special characters?
(2 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
I have a value in this format:
var state = [{"industry-type":"football","your-role":"coach"}]
I would like to output "football". How can I do this?
I've tried state[0].industry-type but it returns an error:
Uncaught ReferenceError: type is not defined
Any help appreciated.
It does not like the '-' in your property name, try:
state[0]['industry-type']
This happens because you can't access a property with - directly.
var state = [{"industry-type":"football","your-role":"coach"}];
console.log(state[0]['industry-type']);
The - symbol is reserved in Javascript, you cannot use it to refer to an object's property because Javascript thinks you're trying to do subtraction: state[0].industry - type; Hence the error "Uncaught ReferenceError: type is not defined" - it is looking for a variable named type to subtract with, which it can't find.
Instead, refer to it by:
state[0]['industry-type']
Because in Javascript, object.property and object['property'] are equal.
For what it's worth, if you have control over those names, it is best practice in Javascript to name things with Camel Case, so your variable would be defined as:
var state = [{"industryType":"football","yourRole":"coach"}]
Then, you could access it like:
state[0].industryType
In order to be able to use dot notation then your:
...property must be a valid JavaScript identifier, i.e. a sequence of
alphanumerical characters, also including the underscore ("_") and
dollar sign ("$"), that cannot start with a number.
From MDN
Like the other answers pointed out, you have to use square bracket notation to access property names of an object that are not valid JavaScript identifiers.
e.g.
state[0]["industry-type"]
Related SO question:
What characters are valid for JavaScript variable names?
You'll need to use bracket notation for the attribute -
state[0]['industry-type']
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I know this question has been asked a million times, I just can't seem to make the answers I find fix my problem.
All I'm trying to do is return the Value of the key that is specified. They Key is in the variable "t":
RefDataTables.forEach(function(t, ti) {
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].t);
});
I know the ".t" is wrong because that is looking for a property (I'm not sure the right word to call it) named "t"
Since "t = "HandleType"" What I'm trying to accomplish is the equivalent of:
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].HandleType)
You can access it by passing the variable between the square brackets.
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0][t])