1

Possible Duplicate:
javascript test for existence of nested object key

Consider this:

typeof(object.prop1.prop2) == "undefined"

where object.prop1 is undefined

This will output a javascript error, how to handle this situation beautifully?
ie: no multiple if

Community
  • 1
  • 1
The Orca
  • 1,250
  • 2
  • 17
  • 31
  • I had a similar question a while back. Perhaps some of the answers there will help. http://stackoverflow.com/questions/2631001/javascript-test-for-existence-of-nested-object-key – user113716 Oct 26 '10 at 20:11
  • I don't think you can handle this beautifully.... – Pekka Oct 26 '10 at 20:11
  • Does `object && object.prop1 && typeof(object.prop1.prop2) == 'undefined'` count as "beautiful"? – kennytm Oct 26 '10 at 20:12

3 Answers3

0

Wrap inside a try/catch block if you don't want to repeat your if conditions, only way I can think of;

try {
    var x = object.prop1.prop2.prop3.prop4;
} catch(e) {
    console.log("Not enough levels.");
}

Edit,

I don't find this very pretty though... If you could explain your data structure a bit more, maybe one could provide a definitive answer. Are you looking for a property far down the structure? A time ago I wrote some kind of XPath for PHP arrays-function, and it is probably applicable on javascript objects/arrays as well. An attempt for objects (and it works what I can tell):

<html>
<head>
<body>
<script>
window.onload = function() {
    function isObject(o) {
        return (typeof o == 'object' && typeof o.length == 'undefined');
    }

    function object_xpath(object, path) {
        var nodes = path.split('/');
        return object_xpath_helper(object, nodes);
    }

    function object_xpath_helper(object, nodes) {
        if (isObject(object) && nodes.length > 0) {
            var node = nodes.shift();

            if (nodes.length > 0 && isObject(object[node]))
                return object_xpath_helper(object[node], nodes);
            else if (nodes.length == 0)
                return object[node];
        }

        return false;
    }

    var testObject = {
        'one': {
            'two': 'just a string!',
            'three': {
                'four': 'mhmm.',
                'five': {
                    'findMe': 'Here I am!'
                }
            }
        }
    };

    console.log(object_xpath(testObject, 'one/three/five/findMe'));
    console.log(object_xpath(testObject, 'one/three/four/foobar'));

    /* And you should be able to use it in conditional statements as well: */
    if (object_xpath(testObject, 'one/two/nine'))
        console.log("This should never be printed");
    if (object_xpath(testObject, 'one/two'))
        console.log("Found it!");
}
</script>
</body>
</html>
Björn
  • 29,019
  • 9
  • 65
  • 81
0

@Björn's answer is probably the simplest way to do it, but there may be times when you need more information; in which case, you'll just have to test incrementally; eg:

if(foo && foo.bar != undefined && foo.bar.baz != undefined ...)
mway
  • 4,334
  • 3
  • 26
  • 37
0
if(typeof(object.prop1) == "undefined" ? false : (typeof(object.prop1.prop2) == "undefined" ? false : true)) {
  //isnt this beautifull?
}
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73