This is the default behavior of computed properties in JS:
const yourName = 'John';
const resultObject = {
[yourName]: 'Glad you're here, let's celebrate!'
}
The resultObject will be computed to this:
console.log(resultObject)
{
John: 'Glad you\'re here, let\'s celebrate!'
}
NOW... what will happen if yourName is undefined?
const yourName = undefined;
const resultObject = {
[yourName]: 'Glad you\'re here, let\'s celebrate!'
}
console.log(resultObject)
{
undefined: 'Glad you\'re here, let\'s celebrate!'
}
QUESTION:
Is there a way to omit the undefined computed property in the resulting object?
In other words, I'd like resultObject to be:
console.log(resultObject)
{/** void */}