AL-zami AL-zami - 1 year ago 90
Javascript Question

return TRUE or FALSE from a function

Here i have a function .It accepts an array of object and a specific value.The array is iterated using a forEach method to ensure the provided value is already existed in any of the objects in the array.If found it returns FALSE ,or it should return true.But although it returns FALSE ,the rest of the code alse gets executed results in returning TRUE all the time.How i can return only FALSE/TRUE


alert("you already selected the value");

function find_value_in_obj(arr_obj,value){


if(elem.value == value){
console.log('found it ');
return false;
console.log("i am her"); // though value already exists and should returned ,it gets executed results in returning TRUE instead

return true;

Answer Source

forEach iterates over all values. You should be using .some().

And since using some is just a one-liner, you don't actually need to create a helper for it, for example (from MDN):

console.log([2, 5, 8, 1, 4].some(elem => elem > 10));  // false
console.log([12, 5, 8, 1, 4].some(elem => elem > 10)); // true

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download