| | |
How can we delete array in javascript?
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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:
So which will be the best and assured way :
1) OR
2) OR
3) any other ...
Thanks,
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)
var bigArray = ["313123", "123123", "sadasd",.........................];
So which will be the best and assured way :
1)
javascript Syntax (Toggle Plain Text)
delete bigArray;
2)
javascript Syntax (Toggle Plain Text)
bigArray = [];
3) any other ...
Thanks,
When you think you have done a lot, then be ready for YOUR downfall.
Hi everyone,
Lucky,
here's a simple demo, showing how you can removed items inside your array using the
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)
<!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 = []; for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>> bigArray[ i ] = "Item" + i; } alert("bigArray has a total of " + ( bigArray.length ) + " items inside its collection"); var aLen = ( bigArray.length - 1 ); for ( var x = aLen; bigArray[ x ]; x-- ) { // Removing all 500 items from the bigArray. bigArray.pop( x ); } alert( "\nAll items' from the ( bigArray ) has been removed.\nItem count: " + bigArray.length ); }; // --> </script> </head> <body> <div></div> </body> </html>
Last edited by essential; Aug 19th, 2009 at 3:26 am.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
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> Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
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.
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. Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
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.
When you think you have done a lot, then be ready for YOUR downfall.
![]() |
Similar Threads
- PHP array to javascript array (JavaScript / DHTML / AJAX)
- How use php array with javascript array (PHP)
- delete array and assigns array to pointer (C++)
- delete [] array in C# (C#)
- Passing an array from javascript to flash (Graphics and Multimedia)
- Using an array in Javascript (JavaScript / DHTML / AJAX)
- To retrieve the array value in javascript (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Associative Array in Javascript
- Next Thread: how to change id by adding new row..
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically beta box browser bug calendar captchaformproblem cart checkbox close codes column createrange() css cursor date debugger decimal dependent design dom download dropdown element embed enter error events firefox focus form frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsp libcurl listbox maps masterpage media menu microsoft mimic mp4 onmouseover paypal php player position post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search security select size software sql text textarea toggle unicode w3c website window windowofwords windowsxp






