Home > Net >  How I can reset variable value when function end?
How I can reset variable value when function end?

Time:06-09

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

There are many ways to remove element from an array like pop,shift,splice,filter use any of these methods to remove or empty the array.

  • Related