JSFiddle - React, Tailwind, and code Playground

by jakie8

JavaScript

var routes = {
    test: {
        deeper: {
            we: {
                go: function() {
                    console.log('Dayum');
                }
            }
        }
    },
};

var parseObject = (function(path, obj, returnFunc) {
    var paths = path.split('/'),
        check = obj[paths.shift()],
        exists = typeof check != 'undefined',
        isLast = paths.length == 0;

    if (exists) {
        if (isLast) {
            returnFunc.call(undefined, {
                exists: true,
                type: typeof check,
                obj: check
            });
        } else {
            parseObject(paths.join('/'), check, returnFunc);
        }
    } else {
        returnFunc.call(undefined, {
            exists: false,
            obj: false
        });
    }
});

parseObject('test/deeper/we/go', routes, function(res) {
    if (res.exists) {
        if (res.type == "function") {
            res.obj.call();
        }
    } else {
        console.log('Does not exist');
    }
});