简体   繁体   English

Javascript将参数传递给过滤器功能?

[英]Javascript passing arguments to filter function?

I want to remove all elements from the initial array that are of the same value as these arguments. 我想从初始数组中删除与这些参数具有相同值的所有元素。

Ex: 例如:

destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].

Here's my code: 这是我的代码:

function takeaway(value) {

    return ??
}


function destroyer(arr) { 
  // Remove all the values\
  var args = Array.from(arguments);   // args = [[1,2,3,1,2,3],2,3]

  var arr1 = args.shift();            // arr1 = [1, 2, 3, 1, 2, 3]
                                      // args = [2,3]

  var filtered = arr1.filter(takeaway);


  return filtered;

}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

If I'm not mistaken I need to pass the elements that I want to take out (the args array) into the filter function so it knows what to filter out... How would I accompish this? 如果我没记错的话,我需要将要取出的元素( args数组)传递给filter函数,以便它知道要滤除的内容...我将如何实现呢?

Try the includes function of the array. 尝试使用数组的include函数。 If the arr1 element isn't included in the args array it returns false and only the elements that returns true are filtered out. 如果args数组中未包含arr1元素,则它返回false,并且仅过滤掉返回true的元素。

function destroyer(arr) { 
  // Remove all the values\
  var args = Array.from(arguments);   // args = [[1,2,3,1,2,3],2,3]

  var arr1 = args.shift();            // arr1 = [1, 2, 3, 1, 2, 3]
                                      // args = [2,3]

  var filtered = arr1.filter(function(value){
    return !args.includes(value);
});


  return filtered;

}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)
const destroyer = (arr, ...nopes) =>
  arr.filter(value => !nopes.includes(value))

destroyer([1, 2, 3, 1, 2, 3], 2, 3)

Because it's harder to understand the type signature, I'd shy away from using variadic functions though in favor of explicit arrays like this. 因为很难理解类型签名,所以尽管使用了像这样的显式数组,我还是避免使用可变参数函数。 Flipping the argument order would also be better for partial application or currying. 翻转参数顺序对于部分应用或使用curry也更好。

//: ( Array Int, Array Int ) -> Array Int
const destroyer = (nopes, arr) =>
  arr.filter(value => !nopes.includes(value))

destroyer([2, 3], [1, 2, 3, 1, 2, 3])

Use Array#indexOf to check if the element exists in the arguments 使用Array#indexOf检查元素是否存在于参数中

const destroyer = (arr, ...args) => arr.filter(value => args.indexOf(value) === -1);
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3);

This is my solution with a bit of explanation. 这是我的解决方案,并带有一些解释。 It is similar to the others with subtle differences. 它与其他相似,但有细微差别。 It is more detailed showing what the callback function does. 更详细地显示了回调函数的作用。 Hope it helps understand it for everyone! 希望它对大家有所帮助!

let arr = [1, 2, 3, 3, 4, 5]

    function destroyer(myArray) {
        let argsArr = Array.from(arguments)             //argsArr = [[1,2,3,3,4,5],3,4]
        let sliced = argsArr.slice.call(arguments, 1)  //sliced = [3,4] 
                                                        //myArray = [1,2,3,3,4,5]
        function takeAway(val) {                       // val = each element in myArray:1,2,3,3,4,5
            return sliced.indexOf(val) === -1          //[3,4] indexOf 3,4 = true (+1) [3,4] indexOf 1,2,5
                                                        // == false (-1) so it is returned by the filter method
        }
        let answer = myArray.filter(takeAway)           // answer = [1,2,5]
        return answer
    }

    console.log(destroyer(arr, 3, 4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM