Tengo tal función y variable global (como matriz):
const arraysList = [] export const changeColorCategories = (array, draggedColumnId) => { const isColor = arraysList.length ? arraysList[0][0]?.color : []; if (typeof isColor === 'string') { firstLevelColor = isColor; } return array.map((item, index, categories) => { item.color = draggedColumnId !== 3 ? '#010172' : '#000000'; arraysList.push(categories); if (firstLevelColor && !draggedColumnId) { item.color = firstLevelColor; } if (item?.children?.length) { changeColorCategories(item.children); } return item; }) }Cada llamada de esta función envía algunos datos a la matriz. En esta función utilizo la recursividad. Entonces, ¿cómo puedo borrar esta matriz solo cuando esta función termine?
Puede llamar a la función de recursión dentro de otra función de esta manera puede ejecutar lo que quiera cuando finalice la función
const arraysList = [] export const changeColorCategories = (array, draggedColumnId) => { const isColor = arraysList.length ? arraysList[0][0]?.color : []; if (typeof isColor === 'string') { firstLevelColor = isColor; } return array.map((item, index, categories) => { item.color = draggedColumnId !== 3 ? '#010172' : '#000000'; arraysList.push(categories); if (firstLevelColor && !draggedColumnId) { item.color = firstLevelColor; } if (item?.children?.length) { changeColorCategories(item.children); } return item; }) } function runRucFunc(){ const result = changeColorCategories(); //Your other code goes here return result; }Simplemente puede poner su parte de recursión dentro de una función secundaria.
A continuación, llamé a la función inner , también moví arrayList a la función, debido a los cierres, ni siquiera necesita borrar arrayList, se borraría automáticamente a medida que queda fuera del alcance.
p.ej.
export const changeColorCategories = (array, draggedColumnId) => { const arraysList = [] function inner(array, draggedColumnId) { const isColor = arraysList.length ? arraysList[0][0]?.color : []; if (typeof isColor === 'string') { firstLevelColor = isColor; } return array.map((item, index, categories) => { item.color = draggedColumnId !== 3 ? '#010172' : '#000000'; arraysList.push(categories); if (firstLevelColor && !draggedColumnId) { item.color = firstLevelColor; } if (item?.children?.length) { inner(item.children); //we call inner here instead. } return item; }) } // now call our inner // you could do something even before your recursion. const result = inner(array, draggedColumnId); // here we can put what we want after recursion. return result; }Podría envolver la llamada recursiva en otra función así:
const arr = [] const recursive = (counter = 0) => { if(counter === 5) return arr.map((v) => String.fromCodePoint(65 + v)) arr.push(counter) return recursive(++counter) } const go = () => { console.log(recursive()) // [A,B,C,D,E] console.log(arr) // [0,1,2,3,4] arr.length = 0 // clear the array console.log(arr) // [] } go()Alternativamente, si la matriz global en realidad no necesita ser global, y es simplemente un contenedor para la información de trabajo del algoritmo recursivo, entonces podría convertirlo en un parámetro de la función recursiva, que luego quedará fuera del alcance (y será basura). recopilados) cuando finaliza la recursividad.
const recursive = (counter = 0, arr = []) => { if(counter === 5) return arr.map((v) => String.fromCodePoint(65 + v)) arr.push(counter) return recursive(++counter, arr) } console.log(recursive()) // [A,B,C,D,E] console.log(arr) // Error! Not in scope! go()O bien, podría hacer que la función recursiva sea más inteligente y capaz de detectar cuándo está procesando la recursividad final: cómo se hace esto dependerá de la lógica precisa de la función recursiva.