So while trying to set up a new function that I needed to search though a array and return the index if it found a match. I came across something weird and havent been able to find a explanation. For some reason the return inside the forEach does not leave the function allowing the second return to return null.
function findRoomByName(name) {
let index = 0;
roomStatus.forEach(e => {
if(e.name == name){
return index
}
index++
});
return null
}
I have fixed the problem with this code but I want to know why this is happening and if there is a better approach then this. Thank you
const FindRoomByName = (name) => {
let index = 0;
let hit = false
roomStatus.forEach(e => {
if(e.name == name){
hit = true
return index
}
index++
});
if(hit){
return index
}
else{
return null
}
}