Are dashes allowed in javascript property names?
no. the parser will interpret it as the subtract operator.
you can do settings['background-color']
.
How do I reference a JavaScript object property with a hyphen in it?
Look at the comments. You will see that for CSS properties, the key notation is not compatible with a number of properties. Using the camel case key notation therefore is the current way:
obj.style-attr // would become
obj["styleAttr"]
Use key notation rather than dot
style["text-align"]
All arrays in JavaScript are objects and all objects are just associative arrays. This means you can refer to a place in an object just as you would refer to a key in an array.
arr[0]
or the object
obj["method"] == obj.method
A couple things to remember when accessing properties this way:
they are evaluated so use strings unless you are doing something with a counter or using dynamic method names.
This means obj[method] would give you an undefined error while obj["method"] would not
You must use this notation if you are using characters that are not allowed in JavaScript variables.
This regex pretty much sums it up:
[a-zA-Z_$][0-9a-zA-Z_$]*
Referencing object's property name with hyphen
You Could use data["foo-bar"]
Instead.
Object key name with dash
You have to put the key in quotes
let myObject = {
'one-property': 'assignee'
}
Destructuring object having hyphen in property name
You can alias the property name using the colon syntax.
let human = {
"first-name": "John",
"last-name": "Doe",
age: 150
};
let { age, "first-name": firstName } = human;
console.log(age, firstName);
How to use hyphen in object property name?
If you want to set/update a data-*
attribute, you need quotes if you set the attribute:
$('.class').attr({"data-toggle": "whatever-value", "data-target": "#id"});
Otherwise it parses it like
data-toggle
meaning
data - toggle
subtracting two variables.
You can also use .data()
with
$('.class').data({"toggle": "whatever-value", "target": "#id"});
but that does not assign data-*
attributes it just stores the data in jQuery's storage system.
how to access an object property that has dash(-) in it
eachItem["team-1"] this should be fine. if its not, could be a different problem
const hero = {
"spider-man" : 1
}
console.log(hero["spider-man"])
Angular2 - why cant use dash or other special characters in property name
Property names can be strings ("foo"
) or identifiers (foo
).
Identifiers can also be used for variables.
Identifiers cannot include a hyphen because that is the minus operator.
amount-discount
means "Amount minus discount" and not "A variable with a hyphen in the name".
Identifiers can't include spaces, +
characters and a host of other characters either. Use a string if you want a property name to include a special character.
How do I correctly include a dash in an object key in javascript?
jQuery.post("http://mywebsite.com/", {"array-key": "hello"});
should be correct. If this doesn't work, the issue is not with your request.
Referencing an object property that has dashes in the name
Your approach with the curly braces wasn't too wrong, but you need a string between the braces:
var_dump($child->{'item--1'});
Related Topics
Why Can't I Update Props in React.Js
Fulfill (Don't Resolve) Promise with Another Promise
Multiple Path Names for a Same Component in React Router
Arrow Function in Object Literal
Javascript: Get Argument Value and Name of Passed Variable
Is There a Sleep Function in JavaScript
What Is Browser.Ignoresynchronization in Protractor
Pass Arguments with Page.Evaluate
Check If Option Is Selected with Jquery, If Not Select a Default
Fix Node Position in D3 Force Directed Layout
Meteor: Proper Use of Meteor.Wrapasync on Server
Trigger Right Click Using Pure JavaScript
How to Check If an Object Has a Key in JavaScript