Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do conditional destructuring or have a fallback?

I have an object that has lots of deeply nested properties. I want to be able to access properties on "MY_KEY" (below), but if that doesn't exist then get "MY_OTHER_KEY". How can I accomplish that?

const {
  X: {
    Y: {
      MY_KEY: {
        Values: segments = []
      } = {}
    } = {}
  } = {}
} = segment;
like image 275
mergesort Avatar asked Oct 18 '17 17:10

mergesort


1 Answers

You could achieve this using a temporal variable inside your destructuring assignment, something like this:

function destructure(segments) {
  const {
    X: {
      Y: {
        MY_OTHER_KEY: _fallback_value = {},
        MY_KEY: {
          Values: segment = []
        } = _fallback_value,
      } = {},
    } = {},
  } = segments;

  return segment;
}

console.log(destructure({})); // []
console.log(destructure({X:{}})); // []
console.log(destructure({X:{Y:{MY_KEY:{Values:"A"}}}})); // A
console.log(destructure({X:{Y:{MY_OTHER_KEY:{Values:"B"}}}})); // B
console.log(destructure({X:{Y:{MY_OTHER_KEY:{Values:"C"}, MY_KEY:{Values:"D"}}}})); // D

First of all, this kind of destructuring will attempt to extract the second key all the time, which might have some unintended implications, like a property getter for MY_OTHER_KEY will always run.

However I fail to see the beauty in it. Hiding some control flow inside destructuring is just confusing. I would rather suggest extracting the parent object and use regular property access on it:

function destructure(segments) {
  const {
    X: {
      Y: nested = {},
    } = {},
  } = segments;
  const selected = nested.MY_KEY || nested.MY_OTHER_KEY || {};
  const {
    Values: segment = []
  } = selected;
  return segment;
}

console.log(destructure({})); // []
console.log(destructure({X:{}})); // []
console.log(destructure({X:{Y:{MY_KEY:{Values:"A"}}}})); // A
console.log(destructure({X:{Y:{MY_OTHER_KEY:{Values:"B"}}}})); // B
console.log(destructure({X:{Y:{MY_OTHER_KEY:{Values:"C"}, MY_KEY:{Values:"D"}}}})); // D
like image 155
Tamas Hegedus Avatar answered Oct 03 '22 19:10

Tamas Hegedus