Add a key/value to a typescript object

Asked

Viewed 497 times

1

I have an array object that I want to add in position 0 a key and value in it.

I tried something like:

this.testeobj//meu objeto
this.testeobj[0].push({oi: 'teste});

1 answer

0

Vc wants an array of objects

this.testeobj = [] //seu array de objetos
this.testeobj.push({oi: 'teste});

The push is a property only of the array, it will insert at the last position of your array the value passed which in your case is an object.

The example below passing the position is only when you want to put the value in the exact position of the array

this.testeobj[0] = {oi: 'teste};

I hope I’ve helped

  • Just to be clear to the questioner: the second version does not add an item, but replaces it. That is, if there is something at position 0 of the array, that something will be replaced by the new content.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.