javascript - React TypeError:无法读取未定义的属性“包含”
问题描述
这是从 firebase 数据库中读取的数据的演示,
没有 .includes(''),它工作得很好,但我希望它只显示在 textField 中输入的数据
- 我只发布了重要部分,如果代码太多,StackOverflow 将不会批准我的问题*
const arr = [
{ age: 1, name: 'Raphael' },
{ age: 3, name: 'Inyang' },
{ age: 6, name: 'Ifiok' },
{ age: 8, name: 'Ekpedeme' },
];
<ul style={{ listStyleType: 'none' }}>
{arr.map(function (item) {
return (
<li>
<div
style={{
justifyContent: 'flex-start',
width: 'auto',
fontSize: 'calc(4px + 2vmin)',
display: item.name.includes('Raphael') ? 'flex : 'none',
justifyContent: 'flex-start',
}}>
<p>
{item.name}: {item.age}
</p>
</div>
</li>
);
})}
</ul>
它一直向我显示上述错误,可能是什么问题,请问 还有其他解决方法吗?
解决方案
要回答有关使用过滤器的后续问题,而不是显示设置为无的地图。
const arr = [
{ age: 1, name: 'Raphael' },
{ age: 3, name: 'Inyang' },
{ age: 6, name: 'Ifiok' },
{ age: 8 },
];
const filtered = arr.filter(
item => item.hasOwnProperty('name') && item.name.includes('Raphael')
);
<ul style={{ listStyleType: 'none' }}>
{filtered.map(function (item) {
return (
<li>
<div
style={{
justifyContent: 'flex-start',
width: 'auto',
fontSize: 'calc(4px + 2vmin)',
display: 'flex',
justifyContent: 'flex-start',
}}>
<p>
{item.name}: {item.age}
</p>
</div>
</li>
);
})}
</ul>
在我的手机上,所以没有测试。
编辑:现在还过滤掉没有名称属性的数组条目
推荐阅读
- swiftui - SWIFTUI 按钮或 NavigationLink?
- windows - cmd.exe /C 无法运行命令
- numpy - 将 Sklearn 与 NumPy 和图像一起使用并得到此错误 'setting an array element with a sequence'
- javascript - 获取错误类型错误:创建动态路由时无法读取 null 的属性“hasAttribute”
- javascript - 使用开始和停止按钮循环浏览网页
- sql - 语言“plpgsql”只需要一个 AS 项;
- c - 有没有办法制作一个错误计数器,在特定数量的用户输入后终止程序?
- amazon-web-services - 如何限制对 ALB 的访问,使其仅从 AWS 中的 Ec2 机器读取流量?
- python - 根据时间过滤数据框中的某些行
- css - 从不同服务器访问时网页容器的不同外观(仅限 GOOGE CHROME)