Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
How I can reset variable value when function end?

I have such function and global variable (as array):

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;
    })
} 

Every call of this function push some data to array. In this function I use recursion. So how i can clear this array only when this function will end it's work.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can call the recursion function inside another function this way you can run anything you want when the function ends

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 Relatório

0

You can just put your recursion part inside a sub function.

Below I've called the inner function inner, I've also moved the arrayList into the function, due to closures you wound't even need to clear the arrayList, it would be cleared automatically as it goes out of scope.

eg.

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 Relatório

0

You could wrap the recursive call in another function like so:

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()

Alternatively, if the global array does not actually need to be global, and is merely a container for working information of the recursive algorithm, then you could make it a parameter of the recursive function, which will then fall out of scope (and be garbage collected) when the recursion ends.

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()

Or, you could make the recursive function more intelligent and able to detect when it is processing the final recursion: how this is done will depend on the precise logic of the recursive function.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda