const tree = [{ nodeId: "root1" }, { nodeId: "root2", links: [] }]; function findNodeAndParent(tree: any[], targetId: string, parent: any = null, list: any[] = tree): any { for (let i = 0; i < tree.length; i++) { if (tree[i].nodeId === targetId) return { node: tree[i], parent, list, index: i }; } return null; } console.log(findNodeAndParent(tree, "root1").parent === null);