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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…