Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove elements from Array? [duplicate]

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.

like image 624
Ranjith Avatar asked Jul 18 '12 14:07

Ranjith


People also ask

How do you remove duplicate values from an array?

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.

How do you remove duplicates from a matrix?

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.


2 Answers

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.

like image 158
Pointy Avatar answered Sep 21 '22 12:09

Pointy


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 
like image 28
blueiur Avatar answered Sep 22 '22 12:09

blueiur