axios.post('saveUser', {id, name})
If name is empty how could I exclude it in the param? if I put ({id, name}), isn't the method would still have a property of name undefined?
I don't want this
const obj = {id: 123, name: undefined}
I want this const obj = {id: 123}
but I don't want to create a temporary variable like so
let obj = { id: id }; if(name) obj = { ...obj, name };
letif possibleletis unrelated to mutable objects. It's for a mutable variable. You can doconst obj = {id}; if (name) obj[name] = name;and the variable is immutable. The object isn't. Besides it is a very strange requirement. You can determine whether to have a property or not at creation time but it's usually not worth doing that. If you're creating the object right now, it doesn't really matter whether you're treating it mutably or not - assuming you're doing this in a function, it's still a pure operation.idornameyou have other problems than a superfluous property.