Pat
Pat

Reputation: 2238

Does jQuery have a method for checking all values of an array to see if they meet some criteria?

I'm looking for something similar to Groovy's every() method, which tests every element of a list if it meets some criteria. If they all meet the criteria, the function returns true. Otherwise, false. I've tried something like this:

var arr = [1, 0, 1, 0, 1, 1];
var allOnes = $.grep(arr, function(ind) { 
                   return this == 1;
               }).length == arr.length;

..but its not very clean. I haven't had any luck while searching through the API. Is using grep() the only way to do it?

Upvotes: 0

Views: 182

Answers (5)

George Matiashvili
George Matiashvili

Reputation: 110

here is example of function you can use.

var arr = [1, 0, 1, 0, 1, 1];
var allOnes = arr.check(1);

//this function compares all elements in array and if all meet the criteria it returns true
Array.prototype.chack =  function(cond)
{
   var ln = 0;
   for(i=0; i<this.length; i++)
   {
     if(bond === this[i])
     {
       ln++
     }
   }
   if(ln == this.length)
   return true;
   else
   return false;
}

Upvotes: 1

Chris Pickett
Chris Pickett

Reputation: 2822

You could always implement an allOnes method:

function allOnes(array) {
    var result = [];
    for(var i = 0; i < array.length; i += 1) {
        if (array[i] === 1) { result.push(true); }
    }

    return array.length == result.length;
}

or you could be a bit more abstract and test for true/false:

function all(array) {
    var result = [];
    for(var i = 0; i < array.length; i += 1) {
        if (array[i]) { result.push(true); }
    }

    return array.length == result.length;
}

var arr = [1, 0, 1, 0, 1, 1];
var allOnes = all(arr);

or even better, maybe have a changeable predicate:

function all(array, predicate) {
    var result = [],
        predicate = predicate || function(x) { if (x) { return true; } };

    for(var i = 0; i < array.length; i += 1) {
        if (predicate(array[i])) { result.push(true); }
    }

    return array.length == result.length;
}

var allOnes = all(arr, function(x) { return x === 1; })

Upvotes: -1

Naftali
Naftali

Reputation: 146350

Here is a plugin I made that might make it easier:

(function($) {
    $.fn.allOnes = function() {
        var allVal = true;
        this.each(function(ind, item) {
            if (item != 1) {
                allVal = false;
                return allVal;
            }
        });
        return allVal;
    };
})(jQuery);

var arr = [1, 1, 0, 1, 1, 1];
console.log($(arr).allOnes());

Fiddle: http://jsfiddle.net/maniator/NctND/


The following plugin is an expansion of the above and lets you search for a specific number: http://jsfiddle.net/maniator/bFNnn/

(function($) {
    $.fn.allValue = function(pred) {
        var allOnes = true;
        this.each(function(ind, item) {
            if (item != pred) {
                allOnes = false;
                return allOnes;
            }
        });
        return allOnes;
    };
})(jQuery);

var arr = [1, 1, 1, 1, 1, 1];
console.log($(arr).allValue(1));

Upvotes: 1

Jamiec
Jamiec

Reputation: 136174

How about just turning your working code into a method on array, to ease its reuse:

Array.prototype.every = function(predicate){
 return $.grep(this,predicate).length == this.length;  
}

usage:

alert([1,0,1,0].every(function(i) { return i == 1; }));

Working example: http://jsfiddle.net/59J5A/

Edit: changed to grep

Upvotes: 0

Einacio
Einacio

Reputation: 3532

if it is a plain js array, you have $.grep()

.filter() is for use with jQuery or DOM Elements

Upvotes: 2

Related Questions