Hyphens in Keys of Object

Asked
Active3 hr before
Viewed126 times

7 Answers

object
90%

To answer the question: Use square bracket notation: obj.prop is the same as obj["prop"] so you can access property names using strings and use characters that are forbidden in identifiers. , 2 @Brian You are correct for CSS properties. However, I was answering the original general question "How do I reference a javascript object property with a hyphen in it?" Here is an updated version of your jsfiddle: jsfiddle.net/49vkD/1 – Stoney Aug 19 '11 at 14:48 , Damon, addressing the ambiguity & confusion (reflected also by the divergent answers and the added/removed downvotes depending on interpretation...): did you specifically mean CSS properties, as hinted by your example & assumed by most answers, or any JS properties, in general, as indicated by the title and the lack of a CSS tag? [Yes, I know it's been 7 years. :) ] – Sz. Oct 22 '18 at 20:42 ,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:

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

This regex pretty much sums it up:

[a - zA - Z_$][0 - 9 a - zA - Z_$] *
load more v
88%

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:,Use key notation rather than dot,However this solution only works for CSS properties. For example,,Property names may only contain characters, numbers, the well known $ sign and the _ (thanks to pimvdb).

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"]
load more v
72%

You can use anything as JSON keys, as long as it is valid UTF-8, doesn't contain zero code points, and it would be useful if you could represent the key as a string in the programming language of your choice. I might recommend not to use different Unicode representations of the same string (for example "Ä" written as one or two code points). , Ewan, any JSON parser must be able to handle any string as a key. And how is handling an underscore hard? The parser uses the same code it would use for string data. – gnasher729 Oct 25 at 9:21 ,You can't use it (or at least not easily) in swift Decodable for JSON decoding, because the hypen is not acceptable charater. You can't do for example:,After spending some time in the industry and working a few systems. I don't think there is a best practice or proper casing for JSON keys. The most important aspect of any formatting (casing/code-style/etc) is consistency and team adoption.

if you use hyphens, they can get treated as "minus" unless you bracket them - unaesthetic and extra typing...

    var t = json.phrase_stems.text - 1; //error
    var t = json.phrase_stems["text-1"]; //ok 

But if you need to build the string, you need brackets irrespective

    var myTextNo = 1;
    var t = json.phrase_stems["text" + myTextNo];
load more v
65%

Use key notation rather than dot,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.,CSS properties with a - are represented in camelCase in JavaScript objects. That would be:,You could also use a bracket notation to use the string:

I am using this script to make a style object of all the inherited, etc. styles.

var style = css($(this));
alert(style.width);
alert(style.text - align);
load more v
75%

If you attempt to use a space or a hyphen in your property name while using regular dot notation, you might end up with an error such as:,This is a tutorial on how to add new properties to a JavaScript object. In this guide, I will also show you how to dynamically set a new property.,If you attempt to use a space in the property name, you will probably get the following error:,In order to use spaces or hyphens in your object property names, you must use the square bracket approach:

Take a look at the following object:

//Basic JavaScript object that represents a person.
var person = {
   name: 'Michael Jordan',
   dob: '1963-02-17',
   height: 1.98
}
load more v
40%

Try to set an object property (which isn't previously defined) with a key beginning with a dash.,It's expected that it would work like any other string.,Using that notation is what doesn't work for Vue objects. Can you provide an example of how to set a vue object with a key that starts with a dash?,@fnlctrl thank you ! For people wondering how to do it with an object in data that looks like this

Vue.set(testThing.anobject, '-K1l', "This does work")
load more v
22%

I am trying to retrieve the value like this:,I have an stdClass Object like this:,$objectName->key-west ,but the value returned is 0. Why? and How can I retrieve it as 1?

I have an stdClass Object like this:

stdClass Object([key - west] => 1[disney - land] => 1)
load more v

Other "object-undefined" queries related to "Hyphens in Keys of Object"