Javascript 删除数组中的索引-React.js
大家好,谢谢你们以后的回答。 我已经有好几天的麻烦了,我只想删除一个由表单生成的数组的索引,但我做不到。。。 这是我的代码: 删除:Javascript 删除数组中的索引-React.js,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,大家好,谢谢你们以后的回答。 我已经有好几天的麻烦了,我只想删除一个由表单生成的数组的索引,但我做不到。。。 这是我的代码: 删除: handleDelete(index) { const recipients = this.state.recipients; recipients.splice(index, 1); this.setState({recipients: recipients}) } 对于推送索引和渲染:
handleDelete(index) {
const recipients = this.state.recipients;
recipients.splice(index, 1);
this.setState({recipients: recipients})
}
对于推送索引和渲染:
class SendBookPass extends React.Component {
constructor(props) {
super(props);
this.addNewBeneficiary = this.addNewBeneficiary.bind(this);
this.toggleBeneficiaryInfos = this.toggleBeneficiaryInfos.bind(this);
this.state = {
orderId: "qsdp$az546",
territory_level: "departement",
indexForm: 0,
recipients: [
{
name: "",
firstname: "",
address: "",
additionalAddress: "",
zip: "",
city: "",
numberPassBook: 1,
}
],
export_mode: {
verbose:true
},
expanded: false,
};
}
addNewBeneficiary() {
let editedForm = this.state.recipients;
editedForm.push({
name: "",
firstname: "",
address: "",
additionalAddress: "",
zip: "",
city: "",
numberPassBook: 1,
});
this.setState({
recipients: editedForm,
indexForm: editedForm.length - 1,
});
console.log("editedForm", editedForm);
}
toggleBeneficiaryInfos() {
this.setState({
expanded: !this.state.expanded,
});
}
handleUpdate(newForm) {
}
handleDelete(index) {
const recipients = this.state.recipients;
recipients.splice(index, 1);
this.setState({recipients: recipients})
console.log(recipients);
}
handleSubmit() {
// API
};
render() {
const {
indexForm,
recipients
} = this.state;
const { t } = this.props;
return (
<>
<div className="sendPass">
<h1>{t('SEND_PASS')}</h1>
<form
className='sendPassForm'
onSubmit={this.handleSubmit()}
>
<SendPassFormRender infos={recipients[indexForm]} onUpdate={this.handleUpdate} />
<div className="addBeneficiary" onClick={() => this.addNewBeneficiary()}>
<svg data-name="Layer 1" viewBox="0 0 48 60" x="0px" y="0px" >
<path d="M24,5A19,19,0,1,0,43,24,19,19,0,0,0,24,5Zm0,36A17,17,0,1,1,41,24,17,17,0,0,1,24,41Z"/>
<polygon points="25 14 23 14 23 23 14 23 14 25 23 25 23 34 25 34 25 25 34 25 34 23 25 23 25 14"/>
</svg>
<p>{t('ADD_BENEFICIARY')}</p>
</div>
{console.log("indexform", indexForm)}
{indexForm === 0
? ""
: recipients.map((infos, index) =>
<div key={index} className="listBeneficiary">
{console.log("indexform de",infos)}
<p>{infos.name}</p>
<p>{infos.firstname}</p>
<p>{infos.city}</p>
<div onClick={this.handleDelete.bind(this, index)}>
<svg version="1.1" x="0px" y="0px" viewBox="0 0 100 125" >
<path d="M77.4,29.4c-1.7,0-3.2,1.4-3.2,3.2v51.4c0,2.5-2.1,4.6-4.6,4.6H30.5c-2.5,0-4.6-2.1-4.6-4.6V32.5c0-1.7-1.4-3.2-3.2-3.2 c-1.7,0-3.2,1.4-3.2,3.2v51.4c0,6,4.9,10.9,10.9,10.9h39.1c6,0,10.9-4.9,10.9-10.9V32.5C80.5,30.8,79.1,29.4,77.4,29.4z"/><path d="M53,78.7v-41c0-1.7-1.3-3.2-3-3.2s-3,1.4-3,3.2v41c0,1.7,1.3,3.2,3,3.2S53,80.5,53,78.7z"/><path d="M40,78.7v-41c0-1.7-1.3-3.2-3-3.2s-3,1.4-3,3.2v41c0,1.7,1.3,3.2,3,3.2S40,80.5,40,78.7z"/>
<path d="M66,78.7v-41c0-1.7-1.3-3.2-3-3.2s-3,1.4-3,3.2v41c0,1.7,1.3,3.2,3,3.2S66,80.5,66,78.7z"/>
<path d="M84.2,17H15.8c-1.7,0-3.2,1.3-3.2,3s1.4,3,3.2,3h68.3c1.7,0,3.2-1.3,3.2-3S85.9,17,84.2,17z"/>
<path d="M40.9,11h18.2c1.7,0,3.2-1.3,3.2-3s-1.4-3-3.2-3H40.9c-1.7,0-3.2,1.3-3.2,3S39.1,11,40.9,11z"/>
</svg>
</div>
<BeneficiaryInfos
address={infos.address}
additionalAddress={infos.additionalAddress}
zip={infos.zip}
/>
</div>
)}
<input
type="submit"
value={t('SEND')}
/>
</form>
</div>
</>
);
}
}
export default withTranslation('common')(SendBookPass);
类SendBookPass扩展了React.Component{
建造师(道具){
超级(道具);
this.addNewBeneficiary=this.addNewBeneficiary.bind(this);
this.togglebenginiarynfos=this.togglebenginiarynfos.bind(this);
此.state={
orderId:“qsdp$az546”,
地区层面:“部门”,
索引形式:0,
收件人:[
{
姓名:“,
名字:“,
地址:“,
其他地址:“,
邮政编码:“,
城市:“,
数字手册:1,
}
],
导出模式:{
详细:正确
},
扩展:错,
};
}
addNewBeneficiary(){
让editedForm=this.state.recipients;
editedForm.push({
姓名:“,
名字:“,
地址:“,
其他地址:“,
邮政编码:“,
城市:“,
数字手册:1,
});
这是我的国家({
收件人:editedForm,
indexForm:editedForm.length-1,
});
日志(“editedForm”,editedForm);
}
togglegenerianyFos(){
这是我的国家({
展开:!this.state.expanded,
});
}
handleUpdate(新表单){
}
handleDelete(索引){
const recipients=this.state.recipients;
接受者。剪接(索引,1);
this.setState({recipients:recipients})
控制台日志(收件人);
}
handleSubmit(){
//原料药
};
render(){
常数{
指数形式,
接受者
}=本州;
const{t}=this.props;
返回(
{t('SEND_PASS')}
this.addNewBeneficiary()}>
{t('ADD_受益人')}
{console.log(“indexform”,indexform)}
{indexForm===0
? ""
:recipients.map((信息、索引)=>
{console.log(“indexform de”,infos)}
{infos.name}
{infos.firstname}
{infos.city}
)}
);
}
}
导出带翻译的默认值(“通用”)(SendBookPass);
对于FormRender:
class FormSendPass extends React.Component {
handleChange = (e, key) => {
this.props.infos[key] = e.target.value;
this.props.onUpdate(this.props.infos)
};
componentDidUpdate(prevProps) {
if (prevProps.infos!== this.props.infos) {
const name = document.getElementById("name");
const firstname = document.getElementById("firstname");
const address = document.getElementById("address");
const additionalAddress = document.getElementById("additionalAddress");
const zip = document.getElementById("zip");
const city = document.getElementById("city");
if(this.props.infos && this.props.infos.name) {
name.value = this.props.infos.name
} else {
name.value = ""
}
if(this.props.infos && this.props.infos.firstname === undefined) {
firstname.value = this.props.infos.firstname
} else {
firstname.value = ""
}
if(this.props.infos && this.props.infos.address === undefined) {
address.value = this.props.infos.address
} else {
address.value = ""
}
if(this.props.infos && this.props.infos.additionalAddress === undefined) {
additionalAddress.value = this.props.infos.additionalAddress
} else {
additionalAddress.value = ""
}
if(this.props.infos && this.props.infos.zip === undefined) {
zip.value = this.props.infos.zip
} else {
zip.value = ""
}
if(this.props.infos && this.props.infos.city === undefined) {
city.value = this.props.infos.city
} else {
city.value = ""
}
} else {
return ""
}
}
render() {
const { t } = this.props;
return (
<>
<div className='userPassForm'>
<label>
{t('NAME')}*
<input
id="name"
type='text'
onChange={e => this.handleChange(e, "name")}
/>
</label>
<label>
{t('FIRST_NAME')}*
<input
id="firstname"
type='text'
onChange={e => this.handleChange(e, "firstname")}
/>
</label>
</div>
<div className='addressPassForm'>
<label>
{t('ADDRESS')}*
<input
id="address"
type='text'
onChange={e => this.handleChange(e, "address")}
/>
</label>
<label>
{t('ADDITIONAL_ADDRESS')}
<input
id="additionalAddress"
type='text'
onChange={e => this.handleChange(e, "additionalAddress")}
/>
</label>
</div>
<div className='cityUserPassForm'>
<label>
{t('ZIP')}*
<input
id="zip"
type='float'
onChange={e => this.handleChange(e, "zip")}
/>
</label>
<label>
{t('CITY')}*
<input
id="city"
type='text'
onChange={e => this.handleChange(e, "city")}
/>
</label>
</div>
<div className='numberSubmitPassForm'>
<label>
{t('PASS_BOOK')}
<input
type='number'
disabled={true}
value='1'
/>
</label>
</div>
<p>*{t('REQUIRED_FIELDS')}</p>
</>
);
}
}
export default (FormSendPass);
类FormSendPass扩展了React.Component{
handleChange=(e,键)=>{
this.props.infos[key]=e.target.value;
this.props.onUpdate(this.props.infos)
};
componentDidUpdate(prevProps){
if(prevProps.infos!==this.props.infos){
const name=document.getElementById(“名称”);
const firstname=document.getElementById(“firstname”);
const address=document.getElementById(“地址”);
const additionalAddress=document.getElementById(“additionalAddress”);
const zip=document.getElementById(“zip”);
const city=document.getElementById(“城市”);
if(this.props.infos&&this.props.infos.name){
name.value=this.props.infos.name
}否则{
name.value=“”
}
if(this.props.infos&&this.props.infos.firstname==未定义){
firstname.value=this.props.infos.firstname
}否则{
firstname.value=“”
}
if(this.props.infos&&this.props.infos.address==未定义){
address.value=this.props.infos.address
}否则{
address.value=“”
}
if(this.props.infos&&this.props.infos.additionalAddress==未定义){
additionalAddress.value=this.props.infos.additionalAddress
}否则{
additionalAddress.value=“”
}
if(this.props.infos&&this.props.infos.zip==未定义){
zip.value=this.props.infos.zip
}否则{
zip.value=“”
}
if(this.props.infos&&this.props.infos.city===未定义){
city.value=this.props.infos.city
}否则{
city.value=“”
}
}否则{
返回“”
}
}
render(){
const{t}=this.props;
报税表(
{t('NAME')}*
this.handleChange(e,“name”)}
/>
{t('FIRST_NAME')}*
this.handleChange(e,“firstname”)}
/>
{t('ADDRESS')}*
handleDelete(index) {
this.setState({
recipients: this.state.recipients.filter((x, i) => i !== index
})
}