4

Possible Duplicate:
how to empty an array in JavaScript

var jsonParent = [];

This is my jsonArray I am pushing some elements in the array, but now I want to empty this array and remove all elements from it.

asked Oct 4, 2012 at 7:35
0

3 Answers 3

4

In terms of saving memory and references use

jsonParent.length = 0
answered Oct 4, 2012 at 7:37
Sign up to request clarification or add additional context in comments.

1 Comment

Nothing hacky =) it's javascript
3

If there are no other references to the same array then the easiest thing to do is just assign jsonParent to a new empty array:

jsonParent = [];

But if your code has other references to the same array instance then that would leave those other references with the original (populated) array and jsonParent with a different (empty) array. So if that is possible in your situation and you want to empty the actual array instance that you already have you can do:

jsonParent.length = 0;
// or if you like ugly:
jsonParent.splice(0, jsonParent.length);

(Note also that you are not using JSON here in any way. It's not JSON if it's not a string.)

answered Oct 4, 2012 at 7:57

Comments

1

just assign

jsonParent = [] 

again when you want to remove all elements

answered Oct 4, 2012 at 7:36

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.