ForEach Typescript TS2339 "does not exist in type 'void'"

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 in the same class.

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

      

+3


source to share


1 answer


Usage function

takes this

from the caller (something forEach

in your case); using =>

takes this

from the outside area. Thus, use:

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

      



I recommend the following indications:

+8


source







All Articles