Javascript:从数组中删除特定项,return 没有这些项的原始数组

Javascript: Remove specific items from an array and return the original array without those items

JS 新手。做一个练习,我想从数组中删除一些特定的项目,然后 return 返回没有这些项目的原始数组。我是用一种方法做的,但示例解决方案完全不同,我想尝试理解它。

我的思路是否正确(见下面每行代码后的注释)?我想我明白了,但我不确定我的推理是否正确,或者我是否只是运气好。我想确保下次看到这样的东西时我完全理解。

const pull = (arr, ...args) => {
        // In this case, arr = arra1 and ...args = the values "a" and "c"
    let argState = Array.isArray(args[0]) ? args[0] : args;     
        // I think this is saying, "Is the first item (index[0]) in arra1 included in the list of the arguments that follow (in this case, "a" and "c")?"
        // The value of arra1[0] is "a", which is indeed one of the arguments that follow, so the ternary operator would return false and give you the "args", which are "a" and "c" in this case.
        // "a" and "c" form an array (do they? I may be wrong here), so Array.isArray() would evaluate to true, and therefore argState = true
    let pulled = arr.filter((v, i) => !argState.includes(v));
        // I think this is saying, "Loop through arra1. v is the first value, "a" in this case. "Does argState contain the value "a"? 
        // It does, so it will come back true which will evaluate to false when we apply the "!". So "a" is NOT filtered into the new array called "pulled". 
        // Second loop, does argState contain the value "b"? It does not, so it will come back false, which will evaluate to true when we apply the "!". So "b" IS filtered into the new array called "pulled". 
        // And so on and so forth, and we would end up with "pulled = [ b , b ]" for this line.
    arr.length = 0;
        // I believe this just empties the original arr, or arra1 in this case. So arra1 = [ ]
    pulled.forEach(v => arr.push(v));
        // Now we loop through "pulled" and push each value onto arra1
    return pulled;
        // This will return the original arra1 with the new values [ b, b ]
  };

let arra1 = ['a', 'b', 'c', 'a', 'b', 'c'];
console.log(pull(arra1, 'a', 'c')); // will return ["b","b"]

我的主要困惑来自 !argState.includes(v) 部分。如果我们上一行中的 argStatetruefalse 的值结束,那么我们可以检查 argState 是否包含值 (v) 来自 arra1 数组(即本练习中的“a”、“b”或“c”)。由于 Array.IsArray() 检查,argState 已经设置为值 truefalse,它怎么可能包括这样的值?

How could argState include values like this when it was already set to just the value true or false because of the Array.IsArray() check?

未设置为 truefalse。它被设置为一个数组:

let argState = Array.isArray(args[0]) ? args[0] : args;   

如果 args[0] 是一个数组,它被设置为那个数组。否则,它被设置为整个 args 数组。

这相当于

let argState;
if (Array.isArray(args[0])) {
  argState = args[0];
} else {
  argState = args;
}

这允许呼叫者使用格式

pull(someArr, ['foo', 'bar'])

pull(someArr, 'foo', 'bar')

并使用条件运算符构造 argState 然后收集 ['foo', 'bar'] 的数组,而不管调用者使用的格式如何。

问题已得到解答!我的断开连接发生在这一行:

let argState = Array.isArray(args[0]) ? args[0] : args;

我的脑海里一直在读这行:“如果 args[0] 为真,则 return args[0]。如果为假,则 return args。然后,如果 returned 是数组,则生成 argState = true。如果不是,则生成 argState = false。"

而不是正确的阅读方式:“如果 args[0] 是一个数组,因此为真,return args[0]。如果为假,return args."

这会将 argState 设置为某些值,而不仅仅是 truefalse.

非常感谢@CertainPerformance帮助我认识到我的错误!