1

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"]
};

2 Answers 2

2

You need to check for location before you check for area. Try this:

this.area = _parameters["location"] && _parameters["location"]["area"] ? _parameters["location"]["area"] : this.location["area"];

You should also be able to use the dot notation for your property access:

this.area = _parameters.location && _parameters.location.area ? _parameters.location.area : this.location.area;
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any functional reason to prefer a dot notation over the bracketed notation?
They're semantically equivalent. The dot notation has less syntactic noise, and is more idiomatic JavaScript. The array notation is useful if the property name has a hyphen or other character that would otherwise be illegal, but otherwise the dot notation tends to be easier to read.
2

Should be able to prefix it with a simple check to see if _parameters["location"] is set.

this.area = (_parameters["location"] && typeof _parameters["location"]["area"] !== 'undefined') ? _parameters["location"]["area"] : this.location["area"];

Although this could be shortened to:

this.area = (_parameters["location"] && _parameters["location"]["area"]) ? _parameters["location"]["area"] : this.location["area"];

Since undefined properties evaluate to false in conditionals.

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.