Solved

Best way to reset a global array?

Posted on 2014-04-07
4
176 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now