I'm trying to empty an array containing my drawn coordinates when a button "clear" is pressed.
When I call drawnDivs.clear(), I get an error that it is not a function. drawnDivs is certainly an array, and I have Firebug console.logs printing things out. It's hosted here.
-
1Out of interest, where did you get the idea to use clear()? It doesn't appear to be a part of Javascript.thomasrutter– thomasrutter2010年10月26日 03:55:53 +00:00Commented Oct 26, 2010 at 3:55
-
Ah looks like visual basic :)thomasrutter– thomasrutter2010年10月26日 03:58:03 +00:00Commented Oct 26, 2010 at 3:58
-
a google search got me here: roseindia.net/java/javascript-array/…Nona Urbiz– Nona Urbiz2010年10月26日 04:20:18 +00:00Commented Oct 26, 2010 at 4:20
-
readers might be interested in Array.prototype.fill eg. myArray.fill(null)Bob– Bob2024年07月31日 09:52:32 +00:00Commented Jul 31, 2024 at 9:52
5 Answers 5
Nope, it's not. But drawnDivs.length = 0 should work.
5 Comments
drawnDivs = [];
1 Comment
It was answered in Stack Overflow question How do I empty an array in JavaScript? .
Two examples from the answer:
var A = ['some', 'values', 'here'];
//Method 1
//(This was my original answer to the question)
A = [];
// Method 2 (as suggested by Matthew Crumley)
A.length = 0
And here is a nice write up on these two methods by Dr. Axel Rauschmayer.
1 Comment
2 Comments
while (arr.length > 0) { arr.pop(); }You could alternately use the Prototype library and then, use Prototype's clear() method.