过滤函数没有返回正确的值

filter function isn't return right value

本文关键字:返回 函数 过滤      更新时间:2023-09-26

我的javascript过滤器函数不工作:

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

您的过滤器函数需要返回一个值。我也猜你想要.length > 0,因为没有长度的字符串。最后,filter方法将返回一个新数组,因此您可能希望在a中捕获结果。

最后,它看起来像这样:

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