【发布时间】:2021-09-09 10:11:01
【问题描述】:
我有一个接收参数的组件。
我使用该参数仅比较和过滤掉那些等于参数值的元素。
但是当我使用filter 函数时,它会给出一个空数组。
我在这里做错了什么?
编辑
在过滤器功能中,如果我使用parseInt(this.props.match.params) 它可以工作..
但如果我使用parseInt(this.state.registrationId) 任何想法都行不通..
import React, {Component} from "react";
import {Link} from 'react-router-dom';
import Progress from "../Common/Progress";
class AdmissionProcess extends Component {
state = {
progress: true,
enquiries: [],
registrationId: this.props.match.params.reg_status
}
componentDidMount = () => {
this.loadData()
}
componentDidUpdate = (prevProps) => {
if(prevProps.match.params.reg_status !== this.props.match.params.reg_status) {
this.setState({registrationId: this.props.match.params.reg_status})
console.log("I ran")
}
}
loadData () {
const url = window.domain+"/list-enquiry";
fetch(url)
.then(res => res.json())
.then(result => this.setState({enquiries: [...result], progress: false}))
.catch(err => console.log("err"+ " " +err))
}
checkEnquiries = () => {
const reg2 = this.state.enquiries.filter(enq => enq.registrationId===this.state.registrationId)
console.log(reg2)
}
render() {
this.checkEnquiries()
return(
{/* Jsx code here*/}
)
}
export default AdmissionProcess
【问题讨论】:
-
能否请您登录您的
result获取?? -
嗨@RyanLe,我记录了获取结果,它显示了对象数组......正如预期的那样
-
它们是数字还是字符串?你使用 === 平等
-
"一个未定义的数组":那会是什么?要么是
undefined,要么是一个数组,但不是两者兼而有之。而filter从不返回undefined,那你是什么意思? -
@HDM91 其中之一是字符串,但我也尝试了 parseInt() 函数在字符串中但结果仍然相同
标签: javascript reactjs jsx