javascript - 过滤器包含 false 不是函数
问题描述
我正在我的对象中搜索一个数组,以使用过滤器和包含返回一个仅在我的类别数组中具有匹配值的对象。出于某种原因,每当我尝试运行我的功能时,都会不断
TypeError:无法读取 null 的属性“包含”
或者
TypeError: false 不是函数
如果我在过滤器中使用查找功能。
我究竟做错了什么?
[{
"entryTitle": "What Is Sports Medicine Acupuncture?",
"date": "September 30 2015",
"categories": ["Knee Pain"],
"type": false
}, {
"entryTitle": "Providing Quality Care During The COVID-19 Pandemic",
"date": "March 23 2020",
"categories": null,
"type": false
}, {
"entryTitle": "Correcting Severe Antalgic Posture & Gait",
"date": "May 09 2020",
"categories": ["Back Pain"],
"type": true
}, {
"entryTitle": "The Successful Treatment Of Sciatica And Low Back Pain With Sports Medicine Acupuncture®",
"date": "July 24 2020",
"categories": ["Back Pain"],
"type": true
}, {
"entryTitle": "Treating A Quad Strain For A Super Heavyweight Powerlifter Before The KERN US Open Powerlifting Meet",
"date": "June 28 2018",
"categories": ["Knee Pain"],
"type": true
}, {
"entryTitle": "Treating A High Hamstring Strain Before A Powerlifting Competition Using Sports Medicine Acupuncture®",
"date": "June 05 2020",
"categories": ["Back Pain"],
"type": true
}, {
"entryTitle": "Free Acupuncture Treatments For Veterans Through The Veterans Choice Program",
"date": "June 08 2017",
"categories": ["Back Pain", "Disc Herniation", "Shoulder Pain"],
"type": true
}, {
"entryTitle": "The Treatment Of Whiplash Related Injuries With Acupuncture",
"date": "March 04 2016",
"categories": ["Disc Herniation"],
"type": false
}]
我的功能
const [selected, setSelected] = useState("all")
const [posts, setPosts] = useState(postData)
const filterPosts = value => {
let posts = postData
if (value !== "all") {
posts = postData.filter(post => post.categories.includes(value))
//posts = postData.filter(post => post.categories.find(post.categories === value))
}
setSelected(value)
setPosts(posts)
}
预期成绩
{
"entryTitle": "What Is Sports Medicine Acupuncture?",
"date": "September 30 2015",
"categories": ["Knee Pain"],
"type": false
},
{
"entryTitle": "Treating A Quad Strain For A Super Heavyweight Powerlifter Before The KERN US Open Powerlifting Meet",
"date": "June 28 2018",
"categories": ["Knee Pain"],
"type": true
},
解决方案
在postData
某些情况 post.categories
下null
,因此includes
不适用于此
您需要先检查一下,如果categories
有的话,您可以检查:
postData.filter(post => post.categories ? post.categories.includes("Knee Pain") : false )
或者
postData.filter(post => post.categories && post.categories.includes("Knee Pain"))
您可以运行以下代码段,不,这会对您有所帮助:
const data = [{
"entryTitle": "What Is Sports Medicine Acupuncture?",
"date": "September 30 2015",
"categories": ["Knee Pain"],
"type": false
}, {
"entryTitle": "Providing Quality Care During The COVID-19 Pandemic",
"date": "March 23 2020",
"categories": null,
"type": false
}, {
"entryTitle": "Correcting Severe Antalgic Posture & Gait",
"date": "May 09 2020",
"categories": ["Back Pain"],
"type": true
}, {
"entryTitle": "The Successful Treatment Of Sciatica And Low Back Pain With Sports Medicine Acupuncture®",
"date": "July 24 2020",
"categories": ["Back Pain"],
"type": true
}, {
"entryTitle": "Treating A Quad Strain For A Super Heavyweight Powerlifter Before The KERN US Open Powerlifting Meet",
"date": "June 28 2018",
"categories": ["Knee Pain"],
"type": true
}, {
"entryTitle": "Treating A High Hamstring Strain Before A Powerlifting Competition Using Sports Medicine Acupuncture®",
"date": "June 05 2020",
"categories": ["Back Pain"],
"type": true
}, {
"entryTitle": "Free Acupuncture Treatments For Veterans Through The Veterans Choice Program",
"date": "June 08 2017",
"categories": ["Back Pain", "Disc Herniation", "Shoulder Pain"],
"type": true
}, {
"entryTitle": "The Treatment Of Whiplash Related Injuries With Acupuncture",
"date": "March 04 2016",
"categories": ["Disc Herniation"],
"type": false
}]
console.log(data.filter(post => post.categories ? post.categories.includes("Knee Pain") : false ))
推荐阅读
- java - Spring Integration - SQS 消息的异步服务激活器
- c++ - C++:用整数移动语义
- devextreme - 如何将复选框添加到 dxDataGrid 分组行
- python - 如何为 AOT 编译的 numba 扩展指定目标目录?
- android - 是否可以使用手机的 IMU 来改善用户的步行路径?
- javascript - 如何以角度合并多个数组
- javascript - 如何使用javascript播放具有相同ID的两个音频文件
- wordpress - 迁移到其他域后图标丢失
- c# - 是否可以在 Windows 10 上调整 BLE 连接间隔?
- nginx - 如何在 kubernetes 中为 nginxinc/nginx-ingress 配置添加第三方模块?