javascript 如何清除javascript中的jsonArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12722351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:58:22  来源:igfitidea点击:

How to clear the jsonArray in javascript

javascriptjqueryjqgrid

提问by user1671219

Possible Duplicate:
how to empty an array in JavaScript

可能的重复:
如何在 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.

这是我的 jsonArray 我正在推送数组中的一些元素,但现在我想清空这个数组并从中删除所有元素。

回答by Anton Rudeshko

In terms of saving memory and references use

在节省内存和引用方面使用

jsonParent.length = 0

回答by nnnnnn

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

如果没有其他对同一个数组的引用,那么最简单的方法就是分配jsonParent给一个新的空数组:

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 jsonParentwith 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不同(空)数组。因此,如果在您的情况下这是可能的,并且您想清空已有的实际数组实例,则可以执行以下操作:

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

(另请注意,您在这里没有以任何方式使用 JSON。如果它不是字符串,则它不是 JSON。)

回答by Fabrizio Calderan

just assign

只是分配

jsonParent = [] 

again when you want to remove all elements

当你想删除所有元素时再次