Solved

Best way to reset a global array?

Posted on 2014-04-07
4
178 Views
Last Modified: 2014-04-08
I have a global multi-dimensional array. What is the best way to reset the array (clear out all the values from it)? The size will obviously change each time.
var aa ={};
aa.myArray = [];
//...array gets populated somewhere
aa.clearArray = function(){

// which is better
aa.myArray[];
// or
aa.myArray.length = 0;
// or
aa.myArray = null;

}

Open in new window

0
Comment
Question by:894359
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39984787
what about :
aa.myArray = [];

Open in new window

0
 

Author Comment

by:894359
ID: 39984983
leakim- I was reading that that method only creates a copy and can have pointer issues.

http://davidwalsh.name/empty-array
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39985312
var aa = new Array();
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39985730
leakim- I was reading that that method only creates a copy and can have pointer issues.

I don't create a copy, it create an empty array ([] same as new Array()) and affect it to your variable. The previous object is not "deleted" so stay in memory. It consume more memory but it's faster. Once the Garbage collector remove object with no reference the object is deleted from memory.

Read the comments.

In your snipet I don't see any << B = A >>
If it's global that mean any function can use it so Iif you've somewhere << B = A >> it's to have an instant copy (and not maintain sync between A and B)
The garbage collector will destruct the previous object without no reference... if needed...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question