1

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?

1
  • 1
    try to make a loop through that array Commented Apr 17, 2019 at 18:21

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is the answer I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.