Given the following class, how can I enumerate its properties, i.e. get an output like [station1, station2, station3 ...]
?
I can only see how to enumerate the values of the properties, i.e. [null, null, null]
.
class stationGuide { station1: any; station2: any; station3: any; constructor(){ this.station1 = null; this.station2 = null; this.station3 = null; } }
The basic properties of an object are those items identified by its four-part name (name, type, instance, and version) and also include owner, status, platform, and release.
Using The For-In loop With the For-In Loop we can enumerate all the properties and methods in an object. Let's see a clear example: Example of for-in loop. This is a general approach in enumerating all the properties and methods in an object.
Objects have two types of properties: data and accessor properties.
An object is made of tangible material (the pen is made of plastic, metal, ink). An object holds together as a single whole (the whole pen, not a fog). An object has properties (the color of the pen, where it is, how thick it writes...).
You can enumerate object properties by passing the object as an argument to object.keys (). The result is an array of the enumerable property names in order of appearance in the object. The following code has an object containing days of the week and their numerical value.
It iterates over "enumerable" properties of the object and applies to all objects that have these properties. An enumerable property is a property of an object with true Enumerable value. By calling property.enumerable, you can see whether a property is enumerable. It will return true or false.
Occasionally these are defaults that it pays to remember. TLDR; There is hidden psobject property that contains a properties collection on custom psobjects. This can be used to enumerate the psobject as name/value pairs. Create Sample Object
Underscore.js has a .keys () method to return the object properties as an array. However, since you want to enumerate the properties, you need to loop through the array. You can list the property names during the loop, one after the other. In the following code, we are using Underscore.js via a CDN.
You have two options, using the Object.keys() and then forEach, or use for/in:
class stationGuide { station1: any; station2: any; station3: any; constructor(){ this.station1 = null; this.station2 = null; this.station3 = null; } } let a = new stationGuide(); Object.keys(a).forEach(key => console.log(key)); for (let key in a) { console.log(key); }
(code in playground)
With the Reflect object you are able to to access and modify any object programmatically. This approach also doesn't throw a "Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'" error.
class Cat { name: string age: number constructor(name: string, age: number){ this.name = name this.age = age } } function printObject(obj: any):void{ const keys = Object.keys(obj) const values = keys.map(key => `${key}: ${Reflect.get(obj,key)}`) console.log(values) } const cat = new Cat("Fluffy", 5) const dog = { name: "Charlie", age: 12, weight: 20 } printObject(cat) printObject(dog)
(code in playground)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With