Referencing object's property name with hyphen [duplicate] - javascript

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 6 years ago.
I have defined an object with properties that have hyphen in their names.
var data = {
"foo-bar": "value",
"this-that": "another value"
}
Now I need to reference this property in JS, but both these ways result in syntax error.
console.log( data.foo-bar )
and
console.log( data."foo-bar" )
So my question is. How can I access a property that contains hyphen in the name in JS?
Disclaimer: The server-side functionality require hyphen-naming of the properties and I don't really feel like rewriting somebody else's whole script that takes the input params like this. And yes, I know this current way is not the most clean approach possible.

You Could use data["foo-bar"] Instead.

Related

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

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?

How to make string to object or array format [duplicate]

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.

What is the reason of using $$ while defining object property in JavaScript [duplicate]

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.

Targeting an array of objects in JavaScript [duplicate]

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

How do I get the Value of a specified Key in JavaScript? [duplicate]

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

Categories

Resources