简体   繁体   English

为什么我的数组过滤器返回“未定义不是函数”?

[英]Why does my array filter return 'undefined is not a function'?

I have this code: 我有以下代码:

var options = [{
    "n":   function(a){return a;},
    "l1":   function(a){return a.duration<1;},
    "1to3": function(a){return a.duration>=1 && a.duration<3;},
    "3to6": function(a){return a.duration>=3 && a.duration<=6;},
    "6to10": function(a){return a.duration>=6 && a.duration<=10;},
    "m10": function(a){return a.duration>10;}
}];

var e = document.getElementById('duration');
var selopt = e.options[e.selectedIndex].value;

var arr = arr.filter(options[ selopt ]); //This line returns the error

I get this error at the line I specified. 我在指定的行收到此错误。 The array arr is an object array with a duration value. 数组arr是具有持续时间值的对象数组。 I know that selopt returns the correct value, so I do not know what is wrong. 我知道selopt返回正确的值,所以我不知道出了什么问题。

I know that selopt returns the correct value 我知道selopt返回正确的值

Yes, but options[ selopt ] doesn't. 是的,但options[ selopt ]则没有。

Your options variable holds an array which contains one object, not the object itself. 您的options变量包含一个数组,其中包含一个对象,而不是对象本身。 Either use 无论使用

…options[0][selopt]…

or 要么

var options = { // no [
    …
};

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

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