拼接JavaScript数组

Splice JavaScript array

这是我尝试从数组中动态删除值的尝试

$('.btn-remove').click(function() {
    var players = ["compare","13076","13075","13077","12755"];
    var removePlayer = $(this).data('player');
    var idx = $.inArray(removePlayer, players);
    if (idx != -1) {
        players.splice(idx, 1);
    }
    window.location = "/" + players.join('/');
})

例如,$(this).data('player') 可能等于 13077,我希望它从数组中删除该值,然后重定向到 url附加到 window.location 变量

这里的问题是 .dataplayer 数据字符串值转换为数字:

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation... The string value "100" is converted to the number 100.

在您的示例中,您正在做

$.inArray(13077, ["compare","13076","13075","13077","12755"]);

而不是

$.inArray("13077", ["compare","13076","13075","13077","12755"]);

您必须将数据值转换回字符串(例如,removePlayer += "")或用数字值而不是字符串填充数组。