How to check if object prop exists while of destructuring?

Viewed 559

I have this statement :

const {
    'xsi:Event': {
      'xsi:eventData': [
        {
          'xsi:call': [
            {
              'xsi:extTrackingId': [extTrackingId],
              'xsi:personality': [personality],
              'xsi:internalReleaseCause': [releaseCause],
            },
          ],
        },
      ],
    },
  } = data

I'm parsing complicate object from some api. On top is real example of object. In some cases, i have same structure, but without property 'xsi:internalReleaseCause', so in this case i cant define a value for releaseCause constant.

Question is how to check if 'xsi:internalReleaseCause' prop exists, on the fly?

1 Answers

const data= {
    'Event': {
      'eventData': [
        {
          'call': [
            {
              'extTrackingId': [],
              'personality': [],
              'internalReleaseCause': ['hi'],
            },
          ],
        },
      ],
    },
  }


var { Event: {eventData: [{call:[{extTrackingId:[],personality:[],internalReleaseCause}]}]} } = data;
console.log(internalReleaseCause[0])

we can use this object destruction to identify internalReleaseCause on the fly but you have some special characters (:) in object keys. I am not sure how to escape them but if there is a way you can get rid of them, then the below snippet works and you can identify the object directly. Hope this helps!

Related