@imjano/get_object_in_object
Version:
Through a dot-separated path (example: 'subObject.foo.bar') you can find an object inside another parent object.
31 lines (30 loc) • 978 B
JavaScript
;
/**
*
* @param object where you want to find the result object
* @param path path to find the result object
* @description Given a path divided by dots (example: 'foo.bar') you can find an object inside another parent object
* @example
* const path = 'foo.bar'
* const obj = { foo: { bar: 'hello' } }
* console.log( getObjectInObject( obj, path ) ) //output: 'hello'
*/
Object.defineProperty(exports, "__esModule", { value: true });
function getObjectInObject(object, path) {
if (typeof path == 'string')
return getObjectInObject(object, path.split('.'));
else {
if (!object)
return undefined;
if (path.length <= 0)
return undefined;
if (path.length == 1)
return object[path[0]];
else {
const newObject = object[path[0]];
path.shift();
return getObjectInObject(newObject, path);
}
}
}
exports.default = getObjectInObject;