Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
React array map function counter value always coming same

In my react application I am using the array map() function to generate JSX. In that, I am using useState to set the values to an array. I am using a counter to manage the index of the array.

let c= 0
    arr.map((arr2, index) => {
                    c++ 
                    chk_str = arr2.map((value) => {
                    frc_t_id = value.t_id;
                        return <div class="form-check mb-1">
                            <input type="checkbox" class="form-check-input" value={value.domain} onChange={e => {setData(data.splice(c, 0, {domain: e.target.value}))}} />
                            <label class="form-check-label" for="exampleCheck1">{value.domain}</label>
                        </div>
                    })
    
                    return <div className="FormGroup col-md-12">{chk_str}
                        <select name="status" id="" placeholder="Server Status type" class="custom-select" onChange={e => {console.log(c); data[c].server = e.target.value; setData(data)}}>
                            <option value={t_id}>{host_name}</option>
                            <option value={frc_t_id}>{keys[index]}</option>
                        </select></div>
                })

There are two elements in the first array. Here the problem is that in the dropdown onChange event the counter value c is always 2. Please help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use index of map instead of using let c = 0 because let is block scope so it doesn't work well with map function

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda