The problem of using for in to clear the attributes of objects in vue

use vue+element to do a simple function, that is, a table is to click the add button, enter a piece of data, and then add the input data to a column of the table. As a result, there is a problem. Go directly to the code:

.
 handleAdd() {
      this.dialogFormVisible=false
      console.log(this.form)
      this.tableData.unshift(this.form)
      for(let key in this.form){
        this.form[key]=""
      }
    },
formv-modelformtableDatafor informtableDatajsformtableData

Feb.11,2022

the one who commented has already explained the point

this.tableData.unshift (this.form);

this step requires copying the this.form


Why is it empty? if you break the point, you can see that the form data was assigned before your for in.
because you assign a pointer to form, which points to the place where form data is stored in memory. When you execute for in, form is emptied, which is equivalent to emptying form data in memory. Because the form data referenced in tableData points to the same memory location, it will also reference empty data.

Menu