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

179
Vistas
JS reset nested property object

I am using Vue3 composition API and tried resetting the form with the help of Object.assign. Unfortunately, it doesn't reset the form when there are nested properties.

const initialForm = {
    name: "",
    details: {
        type: ""
    }
};

const form = reactive({ ...initialForm });

const resetForm = () => {
    Object.assign(form, initialForm);
}

Here name is reset but details.type is not reset. What should be the approach to reset form here?

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

0

spread operator ... and Object.assign they do shallow copy ONLY.

which means it does not change nested refernces. so details.type from initialForm is the same used in form.details.type.

for that you need to deep copy / clone the object when assigning to form. also when you do the resetForm.

using https://lodash.com/docs/4.17.15#cloneDeep

const initialForm = {
    name: "",
    details: {
        type: ""
    }
};

let form = reactive( _.cloneDeep(initialForm));

const resetForm = () => {
    form =  _.cloneDeep(initialForm) ;
}

without external package:

const initialForm = {
    name: "",
    details: {
        type: ""
    }
};

let form = {...initialForm, details : {...initialForm.details} } ;

const resetForm = () => {
    form = {...initialForm, details : {...initialForm.details} } ;
}
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