SoFunction
Updated on 2025-03-09

About the difference and description of delete

Difference between delete and

delete and sum are methods to delete arrays or objects.

There is actually no difference between these two methods for the object. The use method will directly delete the object's properties (physical deletion)

let obj = {
name: 'fufu',
  age: 20
}
// delete   => {name: 'fufu'}
// (obj, 'age') => {name: 'fufu'}
// Test discovery for objectsdeleteThere is no difference between

But these two methods are different for arrays.

let arr = [1,2,3,4,5]
delete arr[2]  //[1,2,empty,4,5]
 arr[2]  //[1,2,4,5]
  • deleteIt's just that the deleted element becomes empty/undefined. The key values ​​of other elements remain unchanged. The length of the array remains unchanged. (Logical deletion)
  • It is to delete the element directly, and the length changes. (Physical deletion)

Specific usage of vue

It is a native API for vue

The specific use is to add a property to a specific object, and then add code without further ado.

export default() {
  data() {
    food: {
      name: 'apple' 
    }
  }
}
...
Vue.$set(food, 'count', 1);

This code means insert the count property into the food object and assign it to 1

It is a native API for vue

The above examples are also used as examples

export default() {
  data() {
    food: {
      name: 'apple' 
    }
  }
}
...
Vue.$delete(food, 'name');

This code means to delete the name attribute in the food object

The above is personal experience. I hope you can give you a reference and I hope you can support me more.