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

275
Visualizações
React how to setState array with another array?

I would like to set an array just with the values from another array. Should I use push for this?

well, in this case, I'm trying to get the selectedOptions array and set the selectedTags array just with selectedOptions value

but just push 1 value from the selectedOptions array!

So, someone please can spare a hint on how to map an array and get the values from it?

handleChange = (selectedOptions) => {
    this.setState({ selectedOptions });
      let options = [];

    selectedOptions.map((v) => options.push(v.value));
            this.setState((state) => ({
                selectedTags: options

            }));

        

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

0

Mapping over an array

You can use the Array.map function:

const data = [
{id: 1, value: '1'},
{id: 2, value: '2'},
{id: 3, value: '3'},
{id: 4, value: '4'},
{id: 5, value: '5'},
{id: 6, value: '6'},
{id: 7, value: '7'},
]

const onlyValues = data.map(d => d.value)
console.log(onlyValues)

Adding to an array via spread

const data = [1,2,3,4,5,6,7,8,9,10]
const toBeAdded = 11

const newData = [...data, toBeAdded]
console.log(newData)

const toBeAddedArray = [11,12,13,14,15,16]
const newDataArray = [...data, ...toBeAddedArray]
console.log(newDataArray)

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