this.state.records.filter(record => this.state.id == '' || record.id.includes(this.state.id))
.map(record=> (
<ProjectItem key={record.id} project={record} />
))
当我这样做时,我收到 TypeError: record.id.includes is not a function,includes is String func 但我的 record.id 无法识别
constructor(props){
super(props)
this.state={
records:[],
id:''
};
我从 axios.get() 获取记录[]
它的意思是,records
数组不为空,但不知何故id
没有定义。例如:
records = [
{ score: 4.5 },
...
];
Or:
records = [4.5, 5.5, ...]
解决方案是检查有效性record.id
首先检查它是否包含this.state.id
, 像这样:
this.state.records.filter(record =>
this.state.id == '' || (record.id && record.id.includes(this.state.id))
);
UPDATE: 如果以上方法仍然不起作用,可能是因为您正在 IE 上进行测试。includes
在 IE 中不起作用,所以你可能想使用indexOf
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)