3 回答
TA贡献1921条经验 获得超9个赞
您可以在回调中为对象值添加验证filter。
这里,i?.work = i ? i.work : undefined
const array = [
{
"id": 1,
"name": "Ruan Duarte",
"idade": 11,
"work": {
"id": 2,
"name": "React"
}
},
{
"id": 2,
"name": "Raul Dias",
"idade": 13
},
{
"id": 7,
"name": "Caio Ribeiro",
"idade": 60,
"work": {
"id": 4,
"name": "Rails"
}
},
{
"id": 3,
"name": "Felipe Lima",
"idade": 55
},
{
"id": 4,
"name": "Camila",
"idade": 25,
"work": {
"id": 3,
"name": "Phyton"
}
}
]
const search = 'Phy';
console.log(array.filter((i) => {
return (
i?.work?.name?.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "")
.includes(search.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""))
)
}));
TA贡献1775条经验 获得超8个赞
试试这个解决方案。
array.filter((i) => {
return (
i.work && i.work.name && i.work.name.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "")
.includes(search.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""))
)
})
TA贡献1811条经验 获得超5个赞
array.filter(i =>
i?.work?.name?.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "")
.includes(search.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""))
);
添加回答
举报