This particular method is designed to extract all potential pathways within an object: By making use of this approach, you will receive a list of potential paths that look like
["key1.children.key2", "key1.children.key3", "key1.children.key4", "key1.key5"]
function gatherPaths(base) {
let stack = [];
let result = [];
// Checking for object type
const checkObject = value => typeof value === "object";
stack.push(base);
while (stack.length > 0) {
let currentNode = stack.pop();
if (checkObject(currentNode)) {
Object.entries(currentNode).forEach(([childNodeKey, childNodeValue]) => {
if (checkObject(childNodeValue)) {
const newObj = Object.fromEntries(
Object.entries(childNodeValue).map(([cnk, cnv]) => {
return [`${childNodeKey}.${cnk}`, cnv];
})
);
stack.push(newObj);
} else {
stack.push(`${childNodeKey}`);
}
})
} else {
result.push(currentNode);
}
}
return result.reverse();
}
Feel free to drop a comment if you found this function useful!