我有一个过滤器方法,可以检查 result
是否不包含 this.state.filter
中的值,应将其删除。
我的问题是方法没有删除错误的结果
。它使 null
filter(){
const {fetchData} = this.state;
return fetchData.map((result, index) => (
(result[3] == this.state.filter) ?
result : null //there is my problem
))
}
来自console.log
的屏幕
最佳答案
您必须使用 Array.prototype.filter
而不是 map
来从数组中删除元素:
filter() {
const { fetchData } = this.state;
return fetchData.filter( (subarray) => (
subarray[3] === this.state.filter
));
}
关于javascript - 过滤并删除数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754262/