javascript javascript清理数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8980584/
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 05:12:52  来源:igfitidea点击:

javascript clean array

javascriptjqueryarrays

提问by saomi

I have four product gallery with the possibility to check the detail of each product from each gallery. Every time the user enter in the detail of any product an array is filled with all the products from that gallery. I should tell this is one single page.

我有四个产品库,可以检查每个库中每个产品的详细信息。每次用户输入任何产品的详细信息时,都会用该图库中的所有产品填充一个数组。我应该说这是一页。

I want to completely delete all the records in the array when the user exits the detail view, because when the user enter again in the detail view the array length increments his size.

当用户退出详细视图时,我想完全删除数组中的所有记录,因为当用户再次进入详细视图时,数组长度会增加他的大小。

I already tried arrayName.length =0;and arrayName.length = []and it deleted the previous data, but his size continue incrementing like this:

我已经试过了arrayName.length =0;arrayName.length = []它删除了以前的数据,但他的大小继续增加,如下所示:

1st time detail view is loaded --> arrayName{valA,valB,valC}

第一次加载详细信息视图 --> arrayName{valA,valB,valC}

2nd time detail view is loaded --> arrayName{,,,val1,val2,val3}

第二次加载详情视图 --> arrayName{,,,val1,val2,val3}

what it's supposed to be in the 2nd time is: arrayName{val1,val2,val3}

它应该在第二次是: arrayName{val1,val2,val3}

Any idea in how I can solve this issue??

关于如何解决这个问题的任何想法?

Thanks all

谢谢大家

The solution

解决方案

Thanks all guys. It was my problem.

谢谢大家。这是我的问题。

while($rowDetails = mysql_fetch_array($rsDetails)){
?>
        <script language="javascript">
            arrayProd[pos] = <?php echo $rowDetails['RefArticleID']; ?>;
            pos++;
        </script>
   ... 

The array is filled into a while cycle and it increments the pos. At the same time I re-initialize the array I force the pos to be 0 (zero)

该数组被填充到一个 while 循环中并增加 pos。同时我重新初始化数组我强制pos为0(零)

arrayProd = [];
pos =0;

Thank you all

谢谢你们

回答by SoWeLie

This one is really simple, just re-initialize the array:

这个真的很简单,重新初始化数组即可:

arrayName = [];

回答by Mario J Vargas

Did you try re-initializing the array by setting it to an empty array?

您是否尝试通过将数组设置为空数组来重新初始化数组?

arrayName = [];

回答by RobG

I already tried arrayName.length =0;

我已经尝试过 arrayName.length =0;

That should work, except perhaps for browsers that don't comply with ECMA-262 ed 3 — which shoul be very, very few. In which browser does it fail for you?

这应该可以工作,除了不符合 ECMA-262 ed 3 的浏览器——这应该是非常非常少的。在哪个浏览器中失败?

and arrayName.length = []

和 arrayName.length = []

Presumably you meant (otherwise you would see an error):

大概你的意思是(否则你会看到一个错误):

arrayName = [];

which should work without exception.

这应该无一例外地工作。