Join.js
function safeObjectAccess () {
        var object = arguments[0],
            length = arguments.length,
            args = arguments,
            i,
            results,
            temp;

        if (!isNullOrUndefined(object)) {
            for (i = 1; i < length; i += 1) {
                if (Array.isArray(object)) {//if it's an array find the values from those results
                    results = [];
                    object.forEach(function (subDocument) {
                        temp = safeObjectAccess.apply(
                            safeObjectAccess,
                            [subDocument].concat(Array.prototype.slice.apply(args, [i, length]))
                        );

                        if (Array.isArray(temp)) {
                            results = results.concat(temp);
                        } else {
                            results.push(temp);
                        }
                    });
                    break;
                }
                if (typeof object !== "undefined") {
                    object = object[arguments[i]];
                } else {
                    break;
                }
            }
        }

        return results || object
    }
function safeObjectAccess () {
        var object = arguments[0],
            length = arguments.length,
            args = arguments,
            i,
            results,
            temp;

        if (!isNullOrUndefined(object)) {
            for (i = 1; i < length; i += 1) {
                if (Array.isArray(object)) {//if it's an array find the values from those results
                    results = [];
                    object.forEach(function (subDocument) {
                        temp = safeObjectAccess.apply(
                            safeObjectAccess,
                            [subDocument].concat(Array.prototype.slice.apply(args, [i, length]))
                        );

                        if (Array.isArray(temp)) {
                            results = results.concat(temp);
                        } else {
                            results.push(temp);
                        }
                    });
                    break;
                }
                if (typeof object !== "undefined") {
                    object = object[arguments[i]];
                } else {
                    break;
                }
            }
        }

        return results || object
    }
function safeObjectAccess ( ) { var object = arguments [ 0 ] , length = arguments . length , args = arguments , i , results , temp ; if ( ! isNullOrUndefined ( object ) ) { for ( i = 1 ; i < length ; i += 1 ) { if ( Array . isArray ( object ) ) { //if it's an array find the values from those results results = [ ] ; object . forEach ( function ( subDocument ) { temp = safeObjectAccess . apply ( safeObjectAccess , [ subDocument ] . concat ( Array . prototype . slice . apply ( args , [ i , length ] ) ) ) ; if ( Array . isArray ( temp ) ) { results = results . concat ( temp ) ; } else { results . push ( temp ) ; } } ) ; break ; } if ( typeof object !== "undefined" ) { object = object [ arguments [ i ] ] ; } else { break ; } } } return results || object }
Access an object without having to worry about "cannot access property '' of undefined" errors.
Some extra, necessary and ugly convenience built in is that, if we encounter an array on the lookup
path, we recursively drill down into each array value, returning the values discovered in each of those
paths. It's kind of a headache, but necessary.
@returns The value you were looking for or undefined
Access an object without having to worry about cannot access property of undefined errors . Some extra necessary and ugly convenience built in is that if we encounter an array on the lookup path we recursively drill down into each array value returning the values discovered in each of those paths . It s kind of a headache but necessary .