I'm working with a large TypeScript object and I am hoping to automate certain parts of it to streamline my workflow.
myObject = [
{
id: 0,
price: 100,
isBought: false,
click: () => this.buyItem(100, 0)
}
buyItem (itemCost: number, itemIndex: number) {
if (this.money >= itemCost) {
this.myObject[itemIndex].isBought = true;
}
So far, I've successfully implemented code to automatically update the id
property in the object using the following function:
findIndex() {
var objLength = Object.keys(this.myObject).length;
for(let i=0; i<objLength; i++) {
this.myObject[i].id = i;
}
}
However, what I'm struggling with is passing property values from within the object itself. Essentially, I am aiming for something like this:
myObject = [
{
id: 0,
price: 100,
isBought: false,
click: () => this.buyItem(THIS.PRICE, THIS.ID)
}
Is there a way to achieve this? If not, are there any possible solutions or workarounds that you would recommend?
Thank you.