I am having an issue understanding why Optional Chaining does not work in my code here. I am sure it is my code that is broken, but I would like to learn what the appropriate way to handle this is. In an ideal world, I can do this:
let traitType = trait['trait_type'];
let traitValue = trait['value'];
let configTrait = overrideBids.traitType?.traitValue;
Both traitType
and traitValue
are of type String objects. I get a syntax error with the example above. Instead, what does work is this more verbose code:
let configTrait = null;
if (overrideBids[trait['trait_type']]) {
configTrait = overrideBids[trait['trait_type']][trait['value']];
}
Is there a cleaner way using Optional Chaining or some other modern JavaScript solution to replace the condition I have above to something less verbose?