javascript - 过滤器抛出 - 无法读取 null 的属性“toLowerCase”
问题描述
我正在尝试根据缓存的查询结果过滤给定的输入。由于我再次过滤用户输入值和数据库,我将它们转换为小写并检查
result = this.cachedResults.filter(f => f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1);
它工作正常,直到 cachedResultNULL
在该字段中没有。但是,如果有 NULL 我怎么能在这里逃脱该记录。
解决方案
如果f.prj
是,NULL
那么它不能包含,因此应该过滤掉this.sV
特定的内容,对吗?f
在这种情况下:
result = this.cachedResults.filter(f => f.prj && f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1);
推荐阅读
- c# - 在 C# 中将 StringBuilder 用于常量字符串?
- amazon-web-services - 将 API Gateway 与 CloudFront 一起使用,返回缺少的身份验证令牌
- python - python文件通过io.wrapper错误打开
- javascript - javascript html标签显示不正确的值
- c++ - 在编译时不知道返回类型时如何避免向下转换?
- regex - 如何排除具有特定属性的块内的正则表达式匹配?
- wpf - Set ContentPresenter ContentTemplate on value change in generated field
- kernel - 运行时和内核之间的区别?
- javascript - 将两个列表转换为 json
- r - 响应变量超出范围 -> 在 r LOGNO 中使用 gamlss