Changing Value of the object [closed]

  javascript, reactjs

Hi i have the following Object, basically it is a state in react app. So what i want to acheive is that make a function that can change the value of the keys depending upon what is passed in the function and then update the state.In the function i can send multiple keys to change its value, currently sending showProduct but can send isEditable aswell.Also i need to change the id in the strings.

let obj = { id: "",
    isEditable: false,
    showProduct: false,
    shipment: false,
    create_shipment: false}

This is what i have tried, like checking if the name is equal to key and then manipulate it.Is this the correct way?

const handler = (name,id) => {
  const prevState = {...obj}
  for (let [key, value] of Object.entries(prevState)) {
  if(key === name) {
    key = !value
  }
}
setState(prevState)
}

handler('showProduct',id)

Source: Ask Javascript Questions

LEAVE A COMMENT