Michael Sterling
asked on
How do I destructure this object to get/set one of the properties?
In react I a have created an object that looks like this:
const [allValues, setAllValues] = useState({
username: '',
email: '',
passwordOne: '',
passwordTwo: '',
error: null,
});
After doing some processing, at a certain point, I want to set only the value of the error property. How do I do this?
ASKER
@ste5an: What does accessing that property in code look like? This is my thought:
const {username, email, passwordOne, passwordTwo, error} = [...allValues];
username = "test";
//How do I now get allValues to be...
username: '',
email: '',
passwordOne: '',
passwordTwo: '',
error: 'test'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you really want to change the class by removing properties, then it is a simple delete objectVariable.propertyNam
In a clean OOP approach you would have a second class with an factory method taking your larger source object and returns your new smaller object by internally creating a (deep) copy.