Create a function to be called multiple times in multiple methods

Hello and happy new year everyone! I need a hand with a procedure that I repeat several times in multiple methods, please. I’ll write you an example in short:

method1() {
  //I DO THINGS

  const controlArray = []; 
  this.daySelected.forEach((element) => {
    
    const tStart = element.time.map((t) => t.startIsGreater); 

    if (tStart.includes(true)) {
      
      controlArray.push(tStart); 
      this.setNewEventInfoRequired(false); 
      this.setAlertWarning(true); 
    }
  });
},

method2() {
  //I DO OTHER THINGS DIFFERENT FROM THE FIRST METHOD

  const controlArray = []; 
  this.daySelected.forEach((element) => {
    

    const tStart = element.time.map((t) => t.startIsGreater); 

    if (tStart.includes(true)) {
      controlArray.push(tStart); 
      this.setNewEventInfoRequired(false); 
      this.setAlertWarning(true); 
    }
  });
},
 // and then I repeat the same with other methods (methodFoo, methodBar, etc...)

Everything I write from const controlArray to the end, I repeat the same in multiple methods, so how do I put all that piece of code in one function and just call that function in the methods?

Answer

Just put that common code in one method then call it in every method:

runCommonFunc(){ // give it a significative name
     const controlArray = []; 
  this.daySelected.forEach((element) => {
    

    const tStart = element.time.map((t) => t.startIsGreater); 

    if (tStart.includes(true)) {
      controlArray.push(tStart); 
      this.setNewEventInfoRequired(false); 
      this.setAlertWarning(true); 
    }
  });
},

method1(){
//I do things
this.runCommonFunc()
},

method21(){
//I do things
this.runCommonFunc()
}