Can You Have a Property Name Containing a Dash

Can you have a property name containing a dash

You can't do this with anonymous objects; field names must be valid identifiers. You could instead use a Dictionary, which Json.Net should serialise just as easily as an anonymous object:

var document = new {
conditions = new Dictionary<string, string>() {
{ "acl", "public-read" },
{ "bucket", "s3-bucketname" },
{ "starts-with", "test/path" }
}
};

How do I deal with dashes in a property name in powershell

You can put it in quotes like so:

$sip = $sipaccount.'msRTCSIP-PrimaryUserAddress'

Output a JSON property name with a dash using an anonymous object

Since you are using an anonymous object, the simplest workaround would be to create a Dictionary<string, string> instead:

Output("somename", new Dictionary<string, string> { { "my-name", "somevalue" } });

Serialization of a dictionary as a JSON object in which the dictionary keys are mapped to JSON property names is supported by many .Net JSON serializers including json.net and javascriptserializer and even datacontractjsonserializer when UseSimpleDictionaryFormat = true as noted here.

And if you have values of different types and are using a serializer that supports arbitrary polymorphism during serialization (Json.NET and JavaScriptSerializer do) you could use a Dictionary<string, object>:

Output("somename",
new Dictionary<string, object>
{
{ "my-name", "somevalue" },
{ "my-id", 101 },
{ "my-value", value },
});

Are dashes allowed in javascript property names?

no. the parser will interpret it as the subtract operator.

you can do settings['background-color'].

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 access object property when property name contains - (hyphen)

This is what you need:

$myobject->{'myweird-name'};

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:

  1. 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

  2. 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.

How to access vue props with name contains dash sign?

There is a difference between props and attributes.

A prop is supposed to be specified in component props, it is normalized to camel case and received only by a component that a prop was provided to.

Attributes aren't normalized and can fall through components that are used as root elements. This behaviour is determined by inheritAttrs property in components. It is commonly used to provide HTML attributes to root HTML elements, but can also be used to provide a prop to nearest nested components that accept it.

If GrandSon is supposed to receive clientId prop, it needs to be declared in the component in props. Although it apparently can receive a prop as a fallthrough attribute from GrandFather in this case, it's preferable to explicitly provide it as <GrandSon :client-id="clientId"> in order for it to not depend on a specific hierarchy of root elements.



Related Topics



Leave a reply



Submit