Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Best way to reset a global array?

Posted on 2014-04-07
4
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

722 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