Let's discuss a simple problem involving objects in Javascript. Take for example an object like this:
var obj={
par1:'value1',
par2:'value2'
}
In JavaScript, we can access the values like obj['par1']
. Now, the question is, can we do something similar in TypeScript
with a class structure like this:
export class obj{
par1:string;
par2:string;
}
this.obj['par1']=value1
Update
Introducing the Documents class
export class Documents {
par1: string
par2: string
constructor(par1: string,
par2: string
){
this.par1=par1
this.par2=par2
}
}
Here is my attempt at solving the problem:
private docs:Documents[]=new Documents[
new Documents('value1','value2'),
new Documents('value3','value4')
];
sort(ordertype:string,property:string){
for(let doc of this.docs){
for(let field in doc)
console.log(doc[field])
}
}
Encountered an error:
[ts] Element implicitly has an 'any' type because index expression is not of type 'number'.
I have attempted to cast it to a string but without success.
Update 2
Error identified and corrected. The code above is now functioning correctly.