Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
¿Cómo puedo restablecer el valor de la variable cuando finaliza la función?

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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; }
about 4 years ago · Juan Pablo Isaza Report

0

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; }
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!