I have the following map within a function
mainFunc(){
// other logics
data.map(function (item) {
item.number = Math.round(item.number);
item.total = item.last - item.first;
item.quantity= item?.quantity ? quantityRange(item?.quantity): '';
});
// other logics
}
quantityRange(quantity){
if(quantity){
if(quantity < 100) return "Less Quantity";
if(quantity < 500) return "Average Quantity";
else return "Good Quantity"
}
}
I have the quantityRange() outside the mainFunc() and i am calling it inside the ternary operator inside the map. when i run my code i get the error quantityRange() not defined. can we not use function like this inside the map in typescript?
Any help would be appreciated.
mainFunc(){
// other logics
const self = this; // make sure you are not loosing this
data.map(function (item) {
item.number = Math.round(item.number);
item.total = item.last - item.first;
item.quantity= item?.quantity ? self.quantityRange(item?.quantity): '';
});
// other logics
}
you should call the method with this keyword, to do so you should bind this. There are different ways to do so, one of them is just to save it in variable.