您好,我不知道为什么即使我清除了那些空元素仍然存在(或者清除函数不起作用猜测)。大家可以看看这个。在将另一个值添加到同一个数组中后,我得到了这个数组。我问的是背后发生了什么,而不仅仅是解决代码谢谢
这是一个函数,每当我单击按钮时,它就会将 4 添加到 DVALUE 数组中。
if("q"+idcount+"d" == this.id){
DVALUE[dcount++] = 4;
// alert("D ARRAY"+DVALUE.toString());
}
每当我点击恢复按钮时,它就会删除最后添加的号码
if ("d" === qwer) {
// alert(""+DVALUE.toString());
DVALUE.pop();
cleararrayD(); // also calling this function to remove empty elements when ever this if occurs
}
这是cleararrayD函数
function cleararrayD() {
lens = DVALUE.length, i;
for (i = 0; i < lens; i++) DVALUE[i] && DVALUE.push(DVALUE[i]); // copy non-empty values to the end of the array
DVALUE.splice(0, lens);
}
最佳答案
I am asking whats is happening behind not just solving code
.length
的DVALUE
数组在cleararrayD()
处没有改变作为元素的函数调用是 .push()
编辑为DVALUE
DVALUE
的每个索引的数组之前.splice()
使用原始数组 .length
进行调用在第二个参数处,删除元素 .push()
前面的元素编辑到数组中。
关于javascript - 清除数组中的空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625630/