I have an array of objects like this:
(2) [{…}, {…}]
0: {type: "", label: "first name", title: "", name: "sa", uniquekey: "", …}
1: {type: "text", label: "first name", title: "", name: "asdasd", uniquekey: "", …}
lastIndex: (...)
lastItem: (...)
length: 2
__proto__: Array(0)
This data is being received from parent component.
Also, in my react state,
this.state = {
data: this.props.newdata,
formname: '',
finalData: {}
}
What I am trying to achieve is when I click a onChange() field handler in child component, it sets a value for state formname. The on clicking form onSubmit() handler, I want to merge both formname and data state into state finalData in the form of an object.
I tried using using map() like this, but got confused:
onSubmit = (e) => {
e.preventDefault();
console.log('TEST FORM', this.state.data);
console.log('TEST FORM NAME', this.state.formname);
// console.log(this.setState({
// newform: [...this.state.newform, this.state.formname]
// }), 'added form name');
var finalData = this.state.data.map((item, i) => {
return {
form: item
};
});
console.log(formField);
this.setState({ finalData:finalData })
}
I want to look it something like this:
{
0:{
formname1:'',
[ 0: {a:'', b:''},
1: {a:'', b:''},
]
},
1:{
formname2:'',
[ 0: {a:'', b:''},
1: {a:'', b:''},
]
},
.......
}
Kindly help out
formname1andformname2are not a part of initial state. Where are they coming from? Do you mind editing the state?