ChrisRockGM
ChrisRockGM

Reputation: 438

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. I know that selopt returns the correct value, so I do not know what is wrong.

Upvotes: 1

Views: 5413

Answers (1)

Bergi
Bergi

Reputation: 664630

I know that selopt returns the correct value

Yes, but options[ selopt ] doesn't.

Your options variable holds an array which contains one object, not the object itself. Either use

…options[0][selopt]…

or

var options = { // no [
    …
};

Upvotes: 3

Related Questions