How can I access this
variable from a for-of loop in typescript? Here's my sample for-of loop:
//for each author qs param
for (let author of qsParams.authors)
{
//check for match by id
var matches = this.vm.Authors.filter(x => x.id == author);
//if no match then continue
if (matches.length == 0) continue;
//select author
this.authorSelected(matches[0]);
}
The this
keyword does not have access to the parent class as expected. I did a basic google but did not find the answer for getting a handle to this
within a for-of loop.
UPDATE
I could add the following reference above the for-of loop but it seems kind of hacky:
var that = this;
//for each author qs param
for (let author of qsParams.authors)
{
//check for match by id
var matches = that.vm.Authors.filter(x => x.id == author);
//if no match then continue
if (matches.length == 0) continue;
//select author
that.authorSelected(matches[0]);
}
Do you have a more elegant way than var that=this
; or is this there no better way?