过滤数组但跳过项目

时间:2017-10-19 23:09:02

标签: javascript arrays foreach array-splice

我正在尝试使用以下方法过滤数组,但是我有一个值被跳过!

function filterArray(array,remove){
    
    console.log('Array Length:',array.length)
    console.log("Array:",array);
    console.log('Remove:',remove);
    console.log('------');
    array.forEach(function(e){       
       if(remove.includes(e.serial)){
           console.log('remove this item:',e);
           array.splice(array.indexOf(e),1);
       }       
    });
    
    console.log('New Array Length:',array.length);
    return array;
}

我不知道为什么会这样。请问有人会对这种情况有所了解吗?

这是我的控制台输出

enter image description here

1 个答案:

答案 0 :(得分:1)

假设您不想改变原始数组,可以使用filter:

function filterArray(array, remove){
    return array.filter(e => !remove.includes(e.serial));
}
相关问题