Reputation: 11012
Consider the Array.prototype.every() function ,
how could I get the falsy element ? for example -
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // get the element "5" (falsy)
Upvotes: 0
Views: 94
Reputation: 4446
You could use a filter:
function tooSmall(element){
return element < 10
}
[12, 5, 8, 130, 44].filter(tooSmall) // gives [5, 8]
[12, 5, 8, 130, 44].filter(tooSmall)[0] // gives the first element ie 5
If you want to stick with the too big function then you could use:
function not(fn) {
return function(){
return !fn.apply({},arguments)
}
}
[12, 5, 8, 130, 44].filter(not(isBigEnough)) // [5, 8]
Upvotes: 4
Reputation: 12561
I agree with the other answers/comments that you want to use filter. However I'm interpreting your question differently in another aspect. How I interpret what you're asking is, once you have filtered down to bigEnough
how can you then also filter down to not big enough.
One answer to this would be to use underscore's _.difference
method:
var entireArray = [12, 5, 8, 130, 44];
var bigEnoughVals = entireArray.filter(isBigEnough);
var tooSmallVals = _.difference(entireArray, bigEnoughVals);
function isBigEnough(element, index, array) {
return element >= 10;
}
See http://underscorejs.org/#difference
Upvotes: 1