Tyepscript Basic Question about adding another object/item to an object and the new property is an array

I’m a newbie (total novice)…I created some properties for the myGlobalVariables object and set them as empty. This solution has me using typescript and many React components. I am not allowed to add in different frameworks or libraries.

  export class Variables extends AbstractService {
     static SERVICE_NAME = "service-variables";
     private myGlobalVariables: any = {
        uploads: [],
        anotherObject: {},
        anotherArray: [],
     }

     appendNewProperty(key: string, value) {
        \ need to delete the existing property if it exists

        \ need to add the new property
     }
    }

The item I want to add is an object and I want to add it to myGlobalVariables uploads array. The property of uploads exists right now and it happens to be an empty array… I want to add this new object into the array…

  {name: "fileA.doc", isComplete: true}

I was planning and importing the Variables into a few different tsx files as different components may want to add to this myGlobalVariables.uploads.

I want to keep it generic as I want to be able to add to any of the other properties in myGlobalVariables.

Answer

Did you try like this?

this.myGlobalVariables.uploads.push({name: "fileA.doc", isComplete: true})

Leave a Reply

Your email address will not be published. Required fields are marked *