In Vue 3, if you have one to usereactive
The responsive array created, and you want to clear this array, there are several ways to achieve it. Here are some common methods:
Method 1: Set the length to 0 directly
You can directly set the length of the array to 0, which will clear the contents of the array.
import { reactive } from 'vue'; let arr = reactive([]); // Add some elements to the array(1, 2, 3); (arr); // [1, 2, 3] // Clear the array = 0; (arr); // []
Method 2: Use splice method
You can usesplice
Method to delete all elements in the array.
import { reactive } from 'vue'; let arr = reactive([]); // Add some elements to the array(1, 2, 3); (arr); // [1, 2, 3] // Clear the array(0, ); (arr); // []
Method 3: Use Method
You can also usefill
Method replaces all elements of the array withundefined
, and set the length to 0.
import { reactive } from 'vue'; let arr = reactive([]); // Add some elements to the array(1, 2, 3); (arr); // [1, 2, 3] // Clear the array(undefined); = 0; (arr); // []
Which method to choose depends on your specific needs and code style. Generally speaking, set the length to 0 directly or usesplice
The method is a relatively common and concise approach.
This is the end of this article about vue3 clearing let arr = reactive([]). For more related content about vue3 clearing let arr = reactive([]) please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!