Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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 在if条件下检查索引变量时,Array.Splice()不会删除零索引处的元素_Javascript_Arrays_Array Splice - Fatal编程技术网

Javascript 在if条件下检查索引变量时,Array.Splice()不会删除零索引处的元素

Javascript 在if条件下检查索引变量时,Array.Splice()不会删除零索引处的元素,javascript,arrays,array-splice,Javascript,Arrays,Array Splice,为什么下面的数组没有被删除 var removeableIndex =-1; bookingStatus.bookings.filter(function(item, index) { if(item.id == id){ removeableIndex = index; return true; } return false;

为什么下面的数组没有被删除

var removeableIndex =-1;
        bookingStatus.bookings.filter(function(item, index) {
            if(item.id == id){
                removeableIndex = index;
                return true;
            }
            return false;
        });
        console.log(removeableIndex)
        if(removeableIndex)
            var result = bookingStatus.bookings.splice(removeableIndex,1);
我通过了一系列正确的预订。 过滤器正确匹配。 当removeableIndex为0时,这不会删除项。假设removeableIndex大于零,则它将被删除

下面的代码在所有情况下都能正常工作,包括removeableIndex为0

var removeableIndex =-1;
        bookingStatus.bookings.filter(function(item, index) {
            if(item.id == id){
                removeableIndex = index;
                return true;
            }
            return false;
        });
        console.log(removeableIndex)
        if(removeableIndex > -1)
            var result = bookingStatus.bookings.splice(removeableIndex,1);
唯一的区别是如果(removeableIndex>-1)


我想知道为什么第一组代码只有在索引为零时才删除该项。

当索引为零时,此条件将为false:

if(removeableIndex)
当您将变量用作整个条件时,它将作为布尔值进行计算。其工作原理与:

if(removeableIndex != 0)

了解JavaScript如何将数字计算为布尔值很重要。0计算为false,所有其他数字计算为true

因为您的
removeableIndex
以-1开头,所以它的计算结果将为true。如果筛选器与索引0处的项匹配,则其计算结果将为false

var removeableIndex; // If we leave the variable undefined, it will evaluate false.
bookingStatus.bookings.filter(function(item, index) {
    if(item.id == id){
        removeableIndex = index;
        return true;
    }
    return false;
});
console.log(removeableIndex)
if(removeableIndex || removeableIndex === 0)
// If removeableIndex evaluates to true, or is equal to 0
    var result = bookingStatus.bookings.splice(removeableIndex,1);
    // remove the index
如果将默认值更改为计算结果为false的值,则解决了一半的问题,但还必须检查该值是否为0,因为这将计算结果为false

var removeableIndex; // If we leave the variable undefined, it will evaluate false.
bookingStatus.bookings.filter(function(item, index) {
    if(item.id == id){
        removeableIndex = index;
        return true;
    }
    return false;
});
console.log(removeableIndex)
if(removeableIndex || removeableIndex === 0)
// If removeableIndex evaluates to true, or is equal to 0
    var result = bookingStatus.bookings.splice(removeableIndex,1);
    // remove the index
但是,您应该能够使用以下代码,因为
Array.prototype.filter()
基于回调函数的返回值返回数组

var result = bookingStatus.bookings.filter(function(item, index) {
    return item.id !== id;
});

removeableIndex
0
时,计算
if(removeableIndex)
将为false,因为
0
为false-y值。所以,你应该对它进行如下评估:

if(removeableIndex >= 0)
或者更警惕一点,

var removeableIndex; //Leave the removeableIndex undefined.
//... Do what you are doing
if(type of removeableIndex != "undefined" && removeableIndex >= 0)
有关JavaScript中Truthy/Falsy值的更多信息,请访问此处:

是的,您是正确的,但它将返回一个数组。因此,对于拼接,我需要知道元素的索引,这就是我对读取结果值不感兴趣的原因。