Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!