Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerate properties on an object

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;      } } 
like image 954
George Edwards Avatar asked Oct 23 '16 10:10

George Edwards


People also ask

What are the properties of an object?

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.

What are the different ways to enumerate all properties of an object?

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.

How many types of object properties are there?

Objects have two types of properties: data and accessor properties.

What are the properties of the object explain with the example?

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

How to enumerate object properties in JavaScript?

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.

How to check if an object has an enumerable property?

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.

Is there a way to enumerate psobject properties?

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

How to enumerate the properties of an object in underscore JS?

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.


2 Answers

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)

like image 143
Nitzan Tomer Avatar answered Oct 02 '22 18:10

Nitzan Tomer


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)

like image 36
szuuuken Avatar answered Oct 02 '22 17:10

szuuuken