清除数组中的空元素

时间:2017-08-11 01:31:14

标签: javascript jquery arrays

你好我不知道为什么那些空元素仍然存在,即使我清楚(或清除功能不起作用猜测)。可能你们看看这个。我在相同的数组中添加另一个值之后得到这个数组。我在问什么是不仅仅是解决代码而已经发生了谢谢

mpmath

这是功能,当我点击按钮它将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);
  }

1 个答案:

答案 0 :(得分:1)

Okey我得到了我的问题的答案。正如你所看到它清除数组但我把元素放在错误的索引上。每次我点击按钮 Dcount ++ 添加索引,所以我只是在我的删除功能中执行 Dcount -

DVALUE[dcount++] = 4;

相关问题