forEach Typescript TS2339 “does not exist on type 'void'”

前端 未结 1 1623
孤独总比滥情好
孤独总比滥情好 2021-01-19 15:12

Trying to find the intersection of two arrays, why am I getting TS2339:Property \'collection\' does not exist on type \'void\'?

All arrays are declared

相关标签:
1条回答
  • 2021-01-19 16:00

    Using a function takes the this from the caller (something in forEach in your case); using => takes the this from the outer scope. Thus use:

    this.locations.forEach(location => {
        this.collection.locations.forEach(id => {
            if(location._id === id) {
                this.display.push(location);
            }
        });
    });
    

    I recommend the following readings:

    • A Refresher on this
    • Arrow Functions
    0 讨论(0)
提交回复
热议问题