ES6 code optimization
how to write the following two pieces of code can be more concise and easier to read.
if (res.after.temporary.address) {
this.state.travellerInfo.after.temporary.address = { ...res.after.temporary.address, ...this.state.travellerInfo.after.temporary.address };
}
if (res.after.temporary.people) {
this.state.travellerInfo.after.temporary.people = { ...res.after.temporary.people, ...this.state.travellerInfo.after.temporary.people };
}
if (res.after.temporary.stroke) {
this.state.travellerInfo.after.temporary.stroke = { ...res.after.temporary.stroke, ...this.state.travellerInfo.after.temporary.stroke };
}
if (res.after.temporary.take) {
this.state.travellerInfo.after.temporary.take = { ...res.after.temporary.take, ...this.state.travellerInfo.after.temporary.take };
}
Object.entries(res.after.show_items).forEach(([key, value]) => {
if (key === "people") {
this.state.travellerInfo.after.temporary.people.show = value;
} else if (key === "address") {
this.state.travellerInfo.after.temporary.address.show = value;
} else if (key === "take") {
this.state.travellerInfo.after.temporary.take.show = value;
} else if (key === "stroke") {
this.state.travellerInfo.after.temporary.stroke.show = value;
}
});
const KEYS = ['address', 'people', 'stroke', 'take'];
for (let key of KEYS) {
if (res.after.temporary[key]) {
this.state.travellerInfo.after.temporary[key] = {...res.after.temporary[key], ...this.state.travellerInfo.after.temporary[key]};
}
}
Object.entries(res.after.show_items).forEach(([key, value]) => {
//
this.state.travellerInfo.after.temporary[key].show = value;
//
if (['address', 'people', 'stroke', 'take'].includes(key)) {
this.state.travellerInfo.after.temporary[key].show = value;
}
});
I think the deficiency of your code is that the field is too deep, and there is no big problem with anything else
and then how do you see
setData(value){
if (res.after.temporary[value]) {
this.state.travellerInfo.after.temporary[value] = { ...res.after.temporary[value], ...this.state.travellerInfo.after.temporary[value] }
}
}
this.setData('address') //
fun(type){
Object.entries(res.after.show_items).forEach(([key, value]) => {
if (key === type) {
this.state.travellerInfo.after.temporary[type].show = value;
}
});
}
step by step, change one for reference
let temporary = res.after.temporary
let stateTemporary = this.state.travellerInfo.after.temporary
if (temporary.address) {
stateTemporary.address = {
...temporary.address,
...stateTemporary.address
}
}
if (temporary.people) {
stateTemporary.people = {
...temporary.people,
...stateTemporary.people
}
}
if (temporary.stroke) {
stateTemporary.stroke = {
...temporary.stroke,
...stateTemporary.stroke
}
}
if (temporary.take) {
stateTemporary.take = {
...temporary.take,
...stateTemporary.take
}
}
second step
let temporary = res.after.temporary
let stateTemporary = this.state.travellerInfo.after.temporary
let keys = ['address', 'people', 'stroke', 'take']
keys.forEach(key => {
if (temporary[key]) {
stateTemporary[key] = Object.assign({}, temporary[key], stateTemporary[key])
}
})