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

Herei have a function .It accepts an array of object and a specific value.The array is iterated using

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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742268301a4412218.html

相关推荐

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

    Herei have a function .It accepts an array of object and a specific value.The array is iterated using

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信