Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the jsonArray in javascript [duplicate]

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.

like image 311
user1671219 Avatar asked Feb 19 '23 23:02

user1671219


2 Answers

In terms of saving memory and references use

jsonParent.length = 0
like image 79
Anton Rudeshko Avatar answered Feb 21 '23 13:02

Anton Rudeshko


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.)

like image 34
nnnnnn Avatar answered Feb 21 '23 12:02

nnnnnn