Insert an element into an array that contains an object

Asked

Viewed 801 times

1

Hello, I would like to know how to insert an element inside an object of an array, in this case, when I add with the code below, the element is inserted as another index:

 this.array = [];
 this.array.push(email);
 this.array.push({ Mensagem: res.data[0].Mensagem });

The return I get is this one:

[
  {
    "CodigoPostagem": 25,
    "DataMensagem": "2017-06-28T14:35:00",
    "Emitente": "Item",
    "Titulo": "Mensagem número 25",
    "Visualizado": false
  },
  {
    "Mensagem": "Teste"
  }
]

The "email" has the above item, I just wanted to insert the return of an api inside that object.

Thank you in advance!

  • You want to add that object to Mensagem to the object of email? that’s it?

1 answer

3


You can only assign before doing push:

this.array = [];
email.mensagem = res.data[0].Mensagem;
this.array.push(email);

Browser other questions tagged

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