I have successfully bound values to a view in my code, but I am concerned about the duplicate nested forEach loops that are currently present. I anticipate that Sonarcube will flag this as redundant code. Can anyone advise me on how to refactor this to avoid repetition?
Is there a way to optimize the following repetitive sections and consolidate them into a generic JavaScript function?
response.mainDish.forEach(element => {
this.selectedMainDish.forEach(ele => {
if (ele?.id === element.id) {
this.bindMainDish.push(ele?.mainDish);
}
});
});
response.sideDish1?.forEach(element => {
this.selectedSideDish1List.forEach(ele => {
if (ele.id === element.id) {
this.bindSideDish1.push(ele.sideDish1);
}
});
});
response.sideDish2?.forEach(element => {
this.selectedSideDish2.forEach(ele => {
if (ele.id === element.id) {
this.bindSideDish2.push(ele.sideDish2);
}
});
});