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

104
Visualizações
Trying to map through an array of objects to display different multiple check boxes dynamically

I am trying to map an array of objects in a form based on user input,

Here is the array of objects:

const arrObj= [
    {item_name: 'test name 1', item_id: 'testname1'}
  ]

Then a user will input a number into a number field on the form, when they do, my onChange function takes the input as a parameter and adds another index to the array and increases the integer stored in the string (the 1 in test name, so the next input should be test name 2)

Here is the on change function

 const addItems= (numOfItems) =>{
    
    for(let i=0; i<numOfItems; i++){
      arrObj.push({item_name:`test name ${i+2}`, item_id:`testname${i+2}`})
    }

    //Console logging does show that function works as intended
    console.log(arrObj)

  }

Now for the grand finale, I am trying to map each item to a multi-select checkbox:

<FormGroup>
                  {arrObj.map((item, index) => (
                    <FormControlLabel
                      key={index}
                      control={
                        <Checkbox
                          name={item.item_name}
                          id={item.item_id}
                          color="primary"
                          classes={{
                            root: classes.checkbox,
                            checked: classes.checked,
                          }}
                        />
                      }
                      label={item.item_name}

                //These checked and onChange is for holding the checked state 
                      checked={valTestItemName[index].length > 0 ? valTestItemName[index].includes(item.item_name) : false}
                      onChange={evt => handleTestItemName(evt, index)}
                    />
                  ))}
                </FormGroup>

Only thing popping up is a blank checkbox. But, when logging the arrObj, I see it is populated as intended. Thank you for the help.

Here is my console.log output if it helps:

0: {item_name: 'test name 1', item_id: 'testname1'}
1: {item_name: 'test name 2', item_id: 'testname2'}
2: {item_name: 'test name 3', item_id: 'testname3'}
length: 3
about 4 years ago · Juan Pablo Isaza
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