我尝试删除数组中的对象,我的任务是发送不带ID的列表,因为列表数组中有ID,我想删除该ID,所以在列表中仅回答和问题:{}。为什么我放id是因为我遵循其他人的指示来创建随机id以使添加列表成功。请参阅以下handleadd中的代码
initialState:
this.state = {
post : props.post,post2 : props.post2,list : [],object : {
answer : '',id : '',question : {
question : ''
}
},
handleAdd和handleForm:
handleTextBox = ({target : { name,value,id }}) => {
const newList = this.state.list.map(obj => {
if (obj.id === id) return {...obj,[name] : value,question : { [name] : value }}
return obj
})
this.setState({
list : newList
})
}
handleAdd = e => {
this.setState({
list : [
...this.state.list,{
...this.state.object,id : shortid.generate(),}
]
})
}
组件:
if (questionId === "Create Own Question") {
return <Fragment>
{
this.state.list.map(h => (
<Fragment>
<Col span={24} classname="form-password">
<AtiField name="question" >
<AtiTextbox
id ={h.id}
name="question"
type ="text"
placeholder="Question"
size="sm"
value = {h.question.question}
classname={`pocketbank-form form-control ${this.state.formError.question ? 'is-invalid' : '' }`}
label={<FormattedMessage id="label.form.question" />}
events={{
onChange : this.handleTextBox
}}
/>
{formError.question}
</AtiField>
</Col>
<Col span={24} classname="form-password">
<AtiField name="answer" >
<AtiTextbox
id = {h.id}
name = "answer"
type = "text"
placeholder = "Security Answer"
size = "sm"
value = {h.answer}
classname = {`pocketbank-form form-control ${this.state.formError.securityAnswer ? 'is-invalid' : '' }`}
label={<FormattedMessage id="label.form.answer" />}
events={{
onChange : this.handleTextBox
}} />
</AtiField>
{formError.securityAnswer}
</Col>
</Fragment>
))
}
<Col span={24} classname="form-password">
<AtiField name="paramKey" >
<AtiButton
text="+"
type="primary"
outline={true}
block={false}
events={{ onClick: (event) => this.handleAdd(event) }}
/>
</AtiField>
</Col>
</Fragment>
}
我希望我可以删除一个对象,尤其是id