Javascript QueryObject

Date: 2018-09-26
var queryObject = function(obj, qry) {
    let curObj = obj;
    let result = [];
    let parts = qry.split('.');
    parts.forEach(function(part, i) {
        if (part in curObj) {
            if (i === parts.length - 1) {
                let resultObj = curObj[part];
                let type = resultObj.constructor.name;
                if (type == 'Array') {
                    result = [];
                } else if (type == 'Object') {
                    result = {};
                }
                let keys = Object.keys(resultObj);
                for (let prop in keys) {
                    if (type == 'Array') {
                        result[prop] = resultObj[prop];
                    } else if (type == 'Object') {
                        let propName = keys[prop];
                        result[propName] = resultObj[propName];
                    }
                }
            } else {
                curObj = curObj[part];
            }
        } else {
            return false;
        }
    });
    if (!Array.isArray(result)) {
        return [result];
    }
    return result;
};

 

13980cookie-checkJavascript QueryObject