I want to check if a function returns true for any item in an array within a function in JavaScript? I'm a mere sapling btw.
No dice unfortunately, maybe I'm not being specific enough... I want any to return true if fun identifies the array to be full and any to return false if the array is empty.
function any(arr, fun){
for (var i=0; i<arr.length; i++) {
if (loop(fun)=== 'false') //<--- this is what I'm not sure about
return false;
if (arr.length>0);
return true;
}
}
I want to check if a function returns true for any item in an array within a function in JavaScript? I'm a mere sapling btw.
No dice unfortunately, maybe I'm not being specific enough... I want any to return true if fun identifies the array to be full and any to return false if the array is empty.
function any(arr, fun){
for (var i=0; i<arr.length; i++) {
if (loop(fun)=== 'false') //<--- this is what I'm not sure about
return false;
if (arr.length>0);
return true;
}
}
Share
Improve this question
edited Dec 1, 2015 at 23:15
Mogsdad
45.8k21 gold badges163 silver badges285 bronze badges
asked Oct 27, 2015 at 20:37
tu6619tu6619
391 silver badge6 bronze badges
6
-
1
Try this:
if (fun(arr[i])=== 'false')
Not sure whatfun
returns, string or boolean! – Rayon Commented Oct 27, 2015 at 20:40 -
I’m not sure what that function actually looks like, but maybe
Array.prototype.every
helps. – Sebastian Simon Commented Oct 27, 2015 at 20:41 - Do you want to know if it returns true for any element, or for every element? Your broken code seems to be checking for every, but the text says any. – Barmar Commented Oct 27, 2015 at 20:45
-
According to your new description, seems like you just want to return a boolean that will be
false
ifarr
is empty andtrue
if it has some value in it. In that case you just needfunction any (arr) { return arr.length > 0; }
. No extra undefinedfun
function is needed. – Javier Conde Commented Oct 27, 2015 at 21:32 - I want to know if it returns true for ANY element – tu6619 Commented Oct 27, 2015 at 21:50
4 Answers
Reset to default 4You have a JS array function for that, it is called some. You should use it like this:
arr.some(fun);
it will return true if fun(x) equals true for any element in arr.
You can use Array.prototype.filter()
to reduce the elements based on a function returning true or false.
var func = function(item) {
return item === 'hello';
}
document.write(!!['hello','world','foo','bar'].filter(func).length + '<br>');
document.write(!!['world','foo','bar'].filter(func).length);
You need to call the function and check it's result (not sure what loop is supposed to do but it doesn't seem to help with anything):
function any(arr, fun) {
for (var i = 0; i < arr.length; i++) {
if (fun(arr[i]) === false) { //assuming the function returns a boolean
return false;
}
}
return true; // if we get trough the entire array they must've all been true :)
}
You can also use Array.every:
arr.every(function(item) {
return fun(item);
});
I came up with this: http://jsfiddle/rbroden/k5qq4ngy/
var arr = [
false,
false,
false,
true,
false];
function fun(arrElement){
return arrElement;
}
function any(array){
var bool = false;
for(i = 0; i < array.length; i++){
if(fun(array[i])) bool = true;
}
return bool;
}
alert(any(arr));
which basically
has a placeholder for a boolean to keep track if the condition was ever met
iterates through the array passed to the function
- if the function ever returns a true, the placeholder bool will be changed to true
- at the end of the function, return the result of the function on bool