admin管理员组文章数量:1332638
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 toforEach
, it doesn't have any impact onfind_value_in_obj
. You should look into usingArray#some
. – Felix Kling Commented Sep 18, 2016 at 5:20
3 Answers
Reset to default 2forEach 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;
}
本文标签: javascriptreturn TRUE or FALSE from a functionStack Overflow
版权声明:本文标题:javascript - return TRUE or FALSE from a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268300a2443824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论