4

While performing object destructuring it is possible to provide default values if the key does not exist (i.e. undefinedKey).

If the (string) key does exist, it does not get overwritten (even if empty).

I'm wondering if it's possible to specify an override for an empty string while destructuring. In the example below, I would like emptyKey to print Override for empty key

const testObject = {
  emptyKey: '',
  filledKey: 'Some Initial Value',
};

const {
  emptyKey = 'Override for empty key',
  filledKey = 'Override for filled key',
  undefinedKey = 'Override for undefined key',
} = testObject;

console.log(emptyKey); //''
console.log(filledKey); //'Some Initial Value'
console.log(undefinedKey); //'Override for undefined key'

1
  • I'm trying to define the defaults as early as possible for clarity in a larger function. I know I can just do || comparisons where the value is used, but I hope there is a cleaner way. Commented Nov 17, 2017 at 18:07

2 Answers 2

4

I'm wondering if it's possible to specify an override for an empty string while destructuring.

No, default values are only considered if the property value is undefined. There is no way to override this behavior.


As a workaround, you could build your own little default value handler, such as

function defaults(obj, test, defaults) {
  return Object.keys(defaults).reduce((acc, prop) => {
    acc[prop] = test(obj[prop]) ? defaults[prop] : obj[prop];
    return acc;
  }, {});
}

const testObject = {
  emptyKey: '',
  filledKey: 'Some Initial Value',
};

const {
  emptyKey,
  filledKey,
  undefinedKey,
} = defaults(
  testObject,
  v => v === undefined || v === '',
  {
    emptyKey: 'Override for empty key',
    filledKey: 'Override for filled key',
    undefinedKey:'Override for undefined key',
  }
);

console.log(emptyKey, filledKey, undefinedKey);

Not quite as elegant, but flexible.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, that answers that! Much appreciated!
0

I'm not sure if flatMap already existed 7 year ago, but at least nowadays you could return undefined on a map on your stuff. In case of an object you need to use Object.keys and flatMap, for an array map is enough.

const testObject = {
    emptyKey: '',
    filledKey: 'Some Initial Value',
};

const [
    emptyKey = 'Override for empty key',
    filledKey = 'Override for filled key',
    undefinedKey = 'Override for undefined key',
] = Object.keys(testObject).flatMap(prop => {
    return !testObject[prop] ? undefined : testObject[prop]
});

console.log(emptyKey); //'Override for empty key'
console.log(filledKey); //'Some Initial Value'
console.log(undefinedKey); //'Override for undefined key'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.