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;
    }
});
Mar.15,2022

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])
  }
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a375-e746.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a375-e746.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?