943,569 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 19th, 2009
-1

How can we delete array in javascript?

Expand Post »
Hi all,

I want help from all you wonderful guys. I want to know how can we free memory occupied by an Array. That means all the elements associated with this array should be deleted.

let say:

javascript Syntax (Toggle Plain Text)
  1. var bigArray = ["313123", "123123", "sadasd",.........................];

So which will be the best and assured way :

1)
javascript Syntax (Toggle Plain Text)
  1. delete bigArray;
OR
2)
javascript Syntax (Toggle Plain Text)
  1. bigArray = [];
OR
3) any other ...

Thanks,
Similar Threads
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Aug 19th, 2009
0

Re: How can we delete array in javascript?

wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Aug 19th, 2009
0

Re: How can we delete array in javascript?

Hi everyone,

Lucky,
here's a simple demo, showing how you can removed items inside your array using the pop(index) method.

javascript Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">
  7. <title>www.daniweb.com</title>
  8. <script type="text/javascript">
  9. <!--
  10.  
  11. window.onload = function() {
  12. var bigArray = [];
  13.  
  14. for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>>
  15. bigArray[ i ] = "Item" + i;
  16. } alert("bigArray has a total of " + ( bigArray.length ) + " items inside its collection");
  17. var aLen = ( bigArray.length - 1 );
  18. for ( var x = aLen; bigArray[ x ]; x-- ) { // Removing all 500 items from the bigArray.
  19. bigArray.pop( x );
  20. } alert( "\nAll items' from the ( bigArray ) has been removed.\nItem count: " + bigArray.length );
  21. };
  22. // -->
  23. </script>
  24. </head>
  25. <body>
  26. <div></div>
  27. </body>
  28. </html>
Last edited by essential; Aug 19th, 2009 at 3:26 am.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 19th, 2009
0

Re: How can we delete array in javascript?

i dont think he is looking for such a processor consuming way of deleting the memory. i think delete keyword works better for his needs. or basically setting the array to null.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Aug 19th, 2009
0

Re: How can we delete array in javascript?

Obtaining serkan's adviced of way of freeing the memory and minimized the above application:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--

window.onload = function() {
   var bigArray = [];  
   bigArray2 = [];
   for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>> 
      bigArray[ i ] = "Item" + i;
      bigArray2[ i ] = "Item" + i;
   } 
/*
  You can use the delete operator to delete variables declared implicitly but not those declared with the var statement. 

Output Sample: */

delete bigArray; // ignore's the delete process.

alert( bigArray.length ); // outputs the same length

// either one of the following statement will work, if you have defined it using the var statement.

bigArray = null; // or:

// bigArray = []; // reset's all items...

delete bigArray2; // this one works and delete's the bigArray2 variable.

};
// --> 
</script>
</head>
<body>
<div></div>
</body>
</html>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 19th, 2009
0

Re: How can we delete array in javascript?

Thanks for taking me into consideration
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Aug 19th, 2009
-1

Re: How can we delete array in javascript?

Thanks gentlemen,

So, using 'delete' OR '[]' will delete all the items (and memory) associated with the array.

Please confirm this.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Aug 19th, 2009
-1

Re: How can we delete array in javascript?

Hi lucky,

both ways will provide the same results, the only difference between the two procedure, is that when you use the delete operator, your variable will no longer be accessible within the function unless you redefined it.

Hope we've claimed your needs...

@serkan - welcome , you had a better solution compared to my first post.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 19th, 2009
-1

Re: How can we delete array in javascript?

wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?
We usually encounter this problem when working with embedded devices (settop boxes etc.) where there is very less memory. We need to free memory where ever its possible.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Aug 19th, 2009
-1

Re: How can we delete array in javascript?

Appreciated!
Thanks to all.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Delete all rows from table in javascript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Flash movies slideshow with Javascript





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC