Possible Duplicate:
how to empty an array in JavaScript
How to remove all items from jQuery array?
I have array var myArray = [];
, I want to clear all items in this array on every post back.
We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.
Method : Using loop This task can be performed in brute force manner using loops. In this, we just iterate the list of list using loop and check for the already presence of element, and append in case it's new element, and construct a non-duplicate matrix.
Simplest thing to do is just
myArray = [];
again.
edit — as pointed out in the comments, and in answers to other questions, another "simplest thing" is
myArray.length = 0;
and that has the advantage of retaining the same array object.
you can remove all item in myArray using array length, it's common pattern.
try this
var myArray = [1, 2, 3]; myArray.length = 0; // remove all item
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With