Let's say I have a code scenario like this....
const obj = {
dogs: ["terrier", "labrador", "poodle"],
ages: [4,6,11],
names: ["Fido", "Max", "Spot"]
};
const [data, setData] = useState(obj);
Then, I want to change one of the names in the array to Richard, for example. How can I do this efficiently?
What I've been doing is cloning the object with let clone = JSON.parse(JSON.stringify(obj))
then modifying the clone, then calling setData(clone)
. I feel like there has got to be a better way though where I don't have to clone the object.