Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery如何删除第一个元素之前的逗号?_Javascript_Jquery_Arrays_Push_Comma - Fatal编程技术网

Javascript jQuery如何删除第一个元素之前的逗号?

Javascript jQuery如何删除第一个元素之前的逗号?,javascript,jquery,arrays,push,comma,Javascript,Jquery,Arrays,Push,Comma,我正在使用jQuery push()获取一个数组。但是第一个元素前面有一个逗号。我需要第一个元素不带逗号的数组(其余元素需要逗号)。知道吗 谢谢 function delImg(newThis){ $(newThis).parent().hide(); var Id = $(newThis).siblings('.hidden').html(); Ids.push(Id); alert(Ids); return Ids; } 您正在向数组中添加一个空元

我正在使用jQuery push()获取一个数组。但是第一个元素前面有一个逗号。我需要第一个元素不带逗号的数组(其余元素需要逗号)。知道吗

谢谢

function delImg(newThis){
    $(newThis).parent().hide();
    var Id = $(newThis).siblings('.hidden').html();
    Ids.push(Id);
    alert(Ids);
    return Ids;

}

您正在向数组中添加一个空元素,以便在开始时获得

function delImg(newThis) {
    $(newThis).parent().hide();
    var Id = $(newThis).siblings('.hidden').html();
    if ($.trim(id) !== '') { //push to array if id value is not empty
        Ids.push(Id);
    }
    alert(Ids);
    return Ids;
}


您需要.trim()Id的HTML。@writeToBhuwan ty Sir,但我使用的是
$.trim()
。但是第0个元素仍然没有定义。
function delImg(newThis) {
    $(newThis).parent().hide();
    var Id = $(newThis).siblings('.hidden').html().trim();
    if (Id !== '') {
        Ids.push(Id);
    }
    alert(Ids);
    return Ids;
}