user2634156
user2634156

Reputation: 1751

filter function isn't return right value

My filter function in javascript isn't working:

var a=["apple", ""];
a.filter(function(x){x.length<0});
a// expect to return ["apple"], but get []

Upvotes: 1

Views: 44

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149000

Your filter function needs to return a value. I'm also guessing you want .length > 0 since there is no such thing as a string with a negative length. Finally, the filter method will return a new array, so you'll probably want to capture the result in a.

In the end, it would look something like this:

a = a.filter(function(x){return x.length > 0});

Upvotes: 3

Related Questions