Created
May 28, 2025 22:22
-
-
Save eduardopc/7dd4ed4aacc1d2c98cf213f45888d5b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// type Folder = [number, Array<number>, string]; // [id, subfolders_ids, folder_name] | |
const list = [ | |
[0, [7, 3], "abc"], | |
[0, [], "xyz"], | |
[3, [], "pqr"], | |
[11, [8], "pqr"], | |
[8, [], "def"], | |
[7, [9], "ijk"], | |
[9, [], "lmn"], | |
]; | |
// const printPath = (folderId) => { | |
// const result = []; | |
// const subFolder = list.find((itemList) => itemList?.[0] === folderId); | |
// result.push(subFolder[2]); | |
// list.find((parentItem) => { | |
// if (parentItem[1].includes(folderId)) { | |
// result.push(parentItem[2]); | |
// const item = list.find((parent) => parent[1].includes(parentItem[0])); | |
// if (!item) { | |
// result.splice(0, result.length); | |
// } else { | |
// result.push(item[2]); | |
// } | |
// } | |
// }); | |
// console.log(result.reverse()); | |
// }; | |
const getRootFolders = () => { | |
const rootValues = []; | |
list.filter((rootFolder) => { | |
if (rootFolder[0] === 0) { | |
rootValues.push(rootFolder[2]); | |
} | |
}); | |
return rootValues; | |
}; | |
const pathArray = []; | |
const handleFindPaths = (folderId) => { | |
const finalFolder = list.find((itemList) => itemList?.[0] === folderId); | |
pathArray.push(finalFolder[2]); | |
list.find((parentItem) => { | |
if (parentItem[1].includes(folderId)) { | |
handleFindPaths(parentItem[0]); | |
} | |
}); | |
return pathArray; | |
}; | |
const printPath = (folderId) => { | |
const data = handleFindPaths(folderId); | |
const rootValues = getRootFolders(); | |
const isThereSomeRootValue = data.some((value) => rootValues.includes(value)); | |
const result = isThereSomeRootValue ? data.reverse() : ""; | |
console.log(result); | |
}; | |
printPath(11); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment