Within this function, I am working with an array of objects and a specific value. The function iterates through the array using a forEach method to check if the provided value already exists in any of the objects within the array. If it is found, it should return FALSE; otherwise, it will return true. However, even when it returns FALSE, the rest of the code gets executed which leads to TRUE being returned all the time. How can I ensure that only FALSE/TRUE is returned?
if(find_value_in_obj(all_selected,current.value)){
all_selected.push({select_elem:current,value:current.value});
console.log(all_selected);
}else{
alert("you already selected the value");
}
function find_value_in_obj(arr_obj,value){
arr_obj.forEach(function(elem,index,array){
if(elem.value == value){
console.log('found it ');
return false;
}
});
console.log("i am here"); // although the value already exists and should be returned, this line gets executed resulting in returning TRUE instead
return true;
}