Looking for alternative approaches to avoid using let
in this code snippet due to the risks associated with variable reassignment leading to bugs that are hard to debug. Any suggestions?
function findNodeById<F extends ISomeOptions>(
optionsList: F[],
nodeId: string): F | null {
const searchNode = (nodes: F[]): F | null => {
for (const node of nodes) {
if (node.id === nodeId) {
return node;
}
if (Array.isArray(node.children)) {
const foundNode = searchNode(node.children);
if (foundNode) {
return foundNode;
}
}
}
return null;
};
return searchNode(optionsList);
}