Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
what is the purpose of ...this.props.pokemon and how's it different from this.props.pokemon

I have been working on a simple react project wherein I found this snippet

 let hand1 =[]
    let hand2 =[ ...this.props.pokemon];
    while(hand1.length < hand2.length){
        let randIdx = Math.floor(Math.random()*hand2.length);
        let randPokemon = hand2.splice(randIdx,1)[0];
        hand1.push(randPokemon)
    }

What is the use of ..this.props.pokemon here?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It is Spread syntax (...)

In your case, it deep copy pokemon array from your props and prevent mutate the original array/object

Take a look at this example where spread syntax is not used:

const firstArray = [1, 2];

const secondArray = firstArray;

secondArray[0] = 9;

console.log(firstArray);

And here is when spread syntax is used

const firstArray = [1, 2];

const secondArray = [...firstArray];

secondArray[0] = 9;

console.log(firstArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

let hand2 = [ ...this.props.pokemon]

The above expression takes all the values inside this.props.pokemon and puts it inside the hand2 array.

For eg:

const fruits = ['apple', 'banana']
const breakfast = [...fruits, 'milk']

console.log(breakfast) -> ['apple', 'banana', 'milk']

Whereas if you don't have the spread operator(...). It'll put the whole array there instead of just the values. For eg:

const fruits = ['apple', 'banana']
const breakfast = [fruits, 'milk']

console.log(breakfast) -> [['apple', 'banana'], 'milk']
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda