Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.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 为什么我不能对函数的参数使用过滤器?_Javascript_Functional Programming_Arguments - Fatal编程技术网

Javascript 为什么我不能对函数的参数使用过滤器?

Javascript 为什么我不能对函数的参数使用过滤器?,javascript,functional-programming,arguments,Javascript,Functional Programming,Arguments,我试图检查传递给duckCount的参数是否具有属性“quack”,并返回具有该属性的参数数 为此,我尝试在arguments数组上使用筛选器,但由于某些原因,我无法使用。有人知道为什么吗 var notDuck=Object.create{quack:true} var duck={quack:true} duckCountduck,notDuck//1 函数duckCount{ //解决方案就在这里 console.logarguments return arguments.filtera

我试图检查传递给duckCount的参数是否具有属性“quack”,并返回具有该属性的参数数

为此,我尝试在arguments数组上使用筛选器,但由于某些原因,我无法使用。有人知道为什么吗

var notDuck=Object.create{quack:true} var duck={quack:true} duckCountduck,notDuck//1 函数duckCount{ //解决方案就在这里 console.logarguments return arguments.filterargument=>argument.hasOwnProperty'quack'.length } 是对象,但不是数组,其中as filter是数组函数。使用rest参数将参数对象转换为数组,然后应用数组函数

var notDuck=Object.create{ 庸医:是的 } var duck={ 庸医:是的 } // 1 函数duckCount{ //解决方案就在这里 console.logarguments 返回[…参数].filterargument=>argument.hasOwnProperty'quack.length }
console.logduckCountduck,notDuckbrk的解决方案是最好的方法,如果你能保证你的代码只会在支持ES6的浏览器上运行,这在现在主要意味着不支持IE,但有时你必须支持旧的浏览器。由于使用了箭头函数,这可能不适用于OP,但这可能对其他人有用。如果需要的话,有一个有用的技巧,它几乎不比使用数组分解或数组更麻烦。从:

return Array.prototype.filter.apply(arguments, function(argument) {/* your function goes here */});