问题描述
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不是一个函数,包含是String函数,但是我的record.id无法识别
when i doing it i getting TypeError: record.id.includes is not a function, includes is String func but my record.id does not recognize
constructor(props){
super(props)
this.state={
records:[],
id:''
};
我正在从axios.get()获取记录[]
i am getting records[] from axios.get()
推荐答案
这意味着 records
数组不为空,但未定义 id
.例如:
It means, records
array is not empty, but somehow id
is not defined. For example:
records = [
{ score: 4.5 },
...
];
或者:
records = [4.5, 5.5, ...]
解决方案是先检查 record.id
的有效性,然后再检查其中是否包含 this.state.id
,如下所示:
The solution is checking for validity of record.id
first before checking that it contains this.state.id
, like this:
this.state.records.filter(record =>
this.state.id == '' || (record.id && record.id.includes(this.state.id))
);
更新: 如果上述方法仍然无效,则可能是因为您正在IE上对其进行测试. includes
在IE中不起作用,因此您可能要使用 indexOf
.
UPDATE: If the above still doesn't work, it could be because you're testing it on IE. includes
doesn't work in IE, so you might want to use indexOf
.
这篇关于反应过滤Json数组//TypeError:include不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!