I have a function that has the parameter _parameter. Ideally, I would like it to be able to contain one or many different JSON-formatted values, without requiring them all to be present. I have constructed a series of conditions to check this, and below is one snippet from the set, as this (and its archetype) is throwing the error below. How do I check to see if _parameters["location"]["area"] exists without throwing an error and without using the try...catch as that would result in tremendous redundancy?
What I would like to use is the following code, or some variation as long as it is still a ternary statement:
this.area = typeof _parameters["location"]["area"] !== 'undefined' ? _parameters["location"]["area"] : this.location["area"];
For giggles, I tried these, as well:
this.area = _parameters["location"]["area"] !== undefined ? _parameters["location"]["area"] : this.location["area"];
if(_parameters["location"]["area"]) {
alert(true);
} else {
alert(false);
}
However, these both return the following error and no alert() menu is ever seen:
"Uncaught TypeError: Cannot read property 'area' of undefined"
This throws the same error, but resolves gracefully:
try {
_parameters["location"]["area"];
} catch (e) {
alert(e);
}
EDIT
For clarity, here are some options that _parameter could contain, and an idea of the structure that I'm trying to produce:
this.name = _parameters["name"] !== undefined ? _parameters["name"] : this.name;
this.gender = _parameters["gender"] !== undefined ? _parameters["gender"] : this.gender;
this.location = {
area : typeof _parameters["location"]["area"] !== 'undefined' ? _parameters["location"]["area"] : this.location["area"],
x : _parameters["location"]["x"] !== undefined ? _parameters["location"]["x"] : this.location["x"],
y : _parameters["location"]["y"] !== undefined ? _parameters["location"]["y"] : this.location["y"]
};