I am interested in iterating through an array of objects and accessing the property values of each object in TypeScript.
In C#, this can be easily done using a foreach loop on the array.
However, it seems a bit different in TypeScript. While we can use a foreach loop, we don't have direct access to the complete object. How can we achieve this?
@Input() gridDefinitions: GridColumnDefinition[]
public test() {
for (var def **in** this.gridDefinitions){
var test = <GridColumnDefinition>this.selectedObject;
let castedtype = <GridColumnDefinition>def; // this gives an error
}
}
UPDATE: I have just discovered the solution. The issue lies in how the collection is being iterated. By using of instead of in, we can access the iterated object directly. Refer to TypeScript for-in statement for more information.