I have the following code (is working well)
const action = {
type: types.CHANGE_ADMIN_USER_PASSWORD,
password: initialState.admin.editUserPassword.password,
confirm: initialState.admin.editUserPassword.confirmPassword,
...input
};
where input
is { password: 'hello' }
or { confirm: 'world' }
. The value can be different, but only one key is present.
Question: is there a way to simplify the code? for example, I would like to have initialState.admin.editUserPassword
only once. UPD: initialState.admin.editUserPassword
has more than two properties, so I can't include all that object to action
.
The easiest way would be destructuring:
const { password, confirmPassword } = initialState.admin.editUserPassword;
const action = {
type: types.CHANGE_ADMIN_USER_PASSWORD,
password,
confirm: confirmPassword,
...input
};
If the nested object (initialState.admin.editUserPassword
) only has those two fields (password
, confirmPassword
), and do not rename confirmPassword
to confirm
you can even do the following:
const action = {
type: types.CHANGE_ADMIN_USER_PASSWORD,
...initialState.admin.editUserPassword,
...input
};