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

405
Vistas
How to remove dynamic key from useState object in React?

How can I remove dynamic key and it's value from react useState object?

I have a checkedItems object. Based on the function call I want to remove the dynamic key and it's value from the object:

  const [checkedItems, setCheckedItems] = useState(null)

    const unCheckItem =(item) => {
    const {id} = item
    setCheckedItems(prevState) => ({
    ...prevState
    // How should I remove that dynamic key which has the value of id?
    })
    }

How can I remove the dynamic key which has the value of id and also remove it's value?

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

0

You can create helper to remove key and use it on previous state, like this:

const removeKey = (key, { [key]: _, ...rest }) => rest;
...
...
setCheckedItems(prev => removeKey(id, prev));
about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use this way, it will work 100%!

    let newObject = {};
    for (let [key, value] of Object.entries(checkedItems)) {
        if (key != id) {
            newObject[key] = value;
        }
    }
    setCheckedItems(newObject);
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