Accessing Class Properties with Spaces

Accessing Class Properties with Spaces

You can do it this way:

$object->{'Date Found'}

Class property with space in it

It is possible in that a class namespace is not restricted to only identifiers (and this is intentional in the language). However, there is no syntax other than getattr for accessing such an attribute.

>>> def two_words(self):
... return 'Worked!'
...
>>> Object = type("Object", (), {"two words": two_words})
>>> obj = Object()
>>> "two words" in dir(obj)
True
>>> getattr(obj, "two words")
<bound method two_words of <__main__.Object object at 0x10d070ee0>>
>>> getattr(obj, "two words")()
'Worked!'

It is also possible to create such an attribute with setattr.

How to get class property name with spaces?

You could try a regex, for sample:

foreach (var fieldName in trimedNames)
{
var fieldNameWithSpaces = Regex.Replace(fieldName, "(\\B[A-Z])", " $1");

// you can use fieldNameWithSpaces here...
}

See this post: .NET - How can you split a "caps" delimited string into an array?

how can i access the object properties that have spaces

You can access using bracket notation

data['personal details']

same for all the other keys with spaces as well as with a single word

data['personal details']['name']

but it's better to use .dot notation for single word json keys

data['personal details'].name //  "Loren" 
data.address['temporary address'] // prints "Acn Block Ist Phase"

Accessing property with space in template expression

{{age.data['how  old']}}

or, probably

{{name.age[0].data['how  old']}}

How do I get a space in my property name?

aliases cant have spaces between them , just like any variable name cant have white space, they are considered as bad naming conventions, you can use _(underscore) eg. Proje_Adi

How to access object variable with spaces in it? Typescript

Here you have:

const obj = {
Email: "edwardmuldrew@gmail.com",
ID: "106150111352875619571",
['Image URL']: "https://lh3.googleusercontent.com/a-/AOh14Gi8zfXDXbMG_kvgmIs2yYiAGbuplyapc0gyjXqpTA=s96-c",
Name: "Edward Muldrew",

}

type Obj = typeof obj

export class User implements Obj {
public ID: string;
public Name: string;
public ['Image URL']: string;
public Email: string;
constructor(
id: string,
name: string,
imageUrl: string,
email: string,

) {
this.ID = id;
this.Name = name;
this['Image URL'] = imageUrl;
this.Email = email

}
}

const foo = new User('1', 'John', 'http://example.com/data/index.png', 'sdf@gmai.com')
var result = foo['Image URL'] // string


Related Topics



Leave a reply



Submit