最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - return TRUE or FALSE from a function - Stack Overflow

programmeradmin5浏览0评论

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

   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 her"); // though value already exists and should returned ,it gets executed results in returning TRUE instead

        return true;
    }

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

   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 her"); // though value already exists and should returned ,it gets executed results in returning TRUE instead

        return true;
    }
Share Improve this question edited Sep 18, 2016 at 6:00 Barmar 783k56 gold badges547 silver badges660 bronze badges asked Sep 18, 2016 at 5:17 AL-zamiAL-zami 9,07617 gold badges77 silver badges136 bronze badges 1
  • 3 return false; only returns from the callback you pass to forEach, it doesn't have any impact on find_value_in_obj. You should look into using Array#some. – Felix Kling Commented Sep 18, 2016 at 5:20
Add a ment  | 

3 Answers 3

Reset to default 2

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

forEach cannot be broken in middle of its iteration like a regular for loop. Use a regular for loop instead. Or, if you are using ES6 then you could achieve the same thing by using .find().

function find_value_in_obj(arr_obj,value){
   return !!!arr_obj.find(itm => itm == value);
}

As felix suggested, you could make use of Array.prototype.some also.

function find_value_in_obj(arr_obj,value){
   return !arr_obj.some(itm => itm == value);
}

You can also try using filter, which is more appropriate and clean.

   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){

    var resultArray = arr_obj.filter(function(elem,index,array){
        return elem.value == value;
    });

    if(resultArray.length > 0){
       return false;
    }
    console.log("i am her"); // though value already exists and should returned ,it gets executed results in returning TRUE instead

    return true;
}
发布评论

评论列表(0)

  1. 暂无评论