Solved

Best way to reset a global array?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now