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.

Community
  • 1
  • 1
user1671219
  • 413
  • 3
  • 6
  • 14

3 Answers3

4

In terms of saving memory and references use

jsonParent.length = 0
Anton Rudeshko
  • 1,039
  • 2
  • 11
  • 20
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.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

just assign

jsonParent = [] 

again when you want to remove all elements

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177