Reputation: 13
I have an object of type "Sorteio" and inside it a vector of objects of type "Resultado", specifically 6 Resultados. I'm instantiating them this way:
saveSorteio() {
var data = {
loteria: this.sorteio.loteria,
resultados: [
{
valor: this.sorteio.resultados[0].valor,
animal: this.sorteio.resultados[0].animal
},
{
valor: this.sorteio.resultados[1].valor,
animal: this.sorteio.resultados[1].animal
},
/* ... */
]
};
}
Is there another way to instantiate the 6 at once or do I need to keep calling index by index?
Upvotes: 1
Views: 64
Reputation: 92440
You can use #array.map()
to make an array that pulls out these properties:
saveSorteio() {
var data = {
loteria: this.sorteio.loteria,
resultados = this.sorteio.resultados.map(({valor, animal}) => ({valor, animal}))
/* ... */
};
}
For example:
sorteio = {
resultados: [
{valor: "v1", animal: 'a1', somethingelse:"else"},
{valor: "v2", animal: 'a2', somethingelse:"else"},
{valor: "v3", animal: 'a3', somethingelse:"else"}
]
}
let newArray = sorteio.resultados.map(({valor, animal}) => ({valor, animal}))
console.log(newArray)
Upvotes: 1