The number of row instances in the array

I have an array in jQuery, and I need to count the number of "true" rows in this array, and then make the numOfTrue variable equal to the number of true rows. So, in the array below there are two "true" lines, so numOfTrue will be 2.

var numOfTrue; var Answers = [ "true", "false", "false", "true", "false" ]; 

I'm not sure how to loop through an array in jQuery to count strings. Or do you need a loop?

The number of true lines can vary from 1 to 5.

+8
javascript jquery
source share
10 answers

Using a basic old-fashioned cycle:

 var numOfTrue = 0; for(var i=0;i<Answers.length;i++){ if(Answers[i] === "true") numOfTrue++; } 

or, a reduce

 var numOfTrue = Answers.reduce(function(p,c){ if(c === "true") p++; return p; },0); 

or a filter

 var numOfTrue = Answers.filter(function(x){ return x === "true"; }).length; 
+24
source share

You do not need jQuery for this. Simple for the loop, as shown below, will do the trick,

 var numOfTrue = 0; var Answers = [ "true", "false", "false", "true", "false" ]; for (var i = 0; i < Answers.length; i++) { if (Answers[i] === "true") { //increment if true numOfTrue++; } } 

or even without a cycle, demo

 Answers.toString().match(/true/g).length 
+10
source share

It may not be that convenient, but you can use filtering and then use grep :

 var num = jQuery.grep(Answers, function (a) { return a == "true"; }).length; 
+7
source share

You do not need jQuery for this task. Just Javascript. Jamiec gives the best answer, you can also use filter or reduce functions available for arrays.

filter function:

 var numOfTrue = Answers.filter(function(item){ return item === "true"; }).length 

Here you provide a callback function that runs for each element of the array. If this function returns true , then this element will be in the returned array. Then you get the length of this array (the length of the array ["true", "true"] is 2 )

reduction function:

 var numOfTrue = Answers.reduce(function(counter, item){ return counter + (item === "true" ? 1 : 0); }, 0); 

Reduce the function callback function that is executed for each element of the array. The array element is represented as the second argument, the first is the return value of the previously executed callback. The second argument to the decrease function is the initial value that is provided for the first callback function. If you omit this, then the first element from the array will be provided (in this example, if you forget to add 0 as the second argument, then the result value will be "true0010" string, because instead of adding numbers you will concatenate bites)

+5
source share
 var numOfTrue = 0; for(var i = 0, len = Answers.length;i < len; i++){ if(Answers[i] === "true"){ numOfTrue++; } } 
+1
source share
 var numOfTrue =0; $.each(Answers,function(index,value){ if(value =="true") { numOfTrue++; } }); 

http://jsfiddle.net/3PpJS/

+1
source share

If you want to use jQuery (and tbh, you shouldn't do this), this will work:

 var numOfTrue; var Answers = [ "true", "false", "false", "true", "false" ]; $.each(Answers, function(i, item) { if (item == "true") numOfTrue++; }); 

Javascript equivalent:

 var numOfTrue; var Answers = [ "true", "false", "false", "true", "false" ]; for (var i = 0; i < Answers.length; i++) { if (answers[i] == "true") numOfTrue++; }); 
+1
source share
 var numOfTrue; var Answers = [ "true", "false", "false", "true", "false" ]; var i = 0; $.each(Answers, function(index, value){ if(value == "true") { i++; } }); alert(i); 

http://jsfiddle.net/kpcrash/Xxcw6/

+1
source share

You can use jQuery.each to iterate over the list, for example:

 var Answers = [ "true", "false", "false", "true", "false" ]; var numOfTrue = countTrue(Answers); alert(numOfTrue); function countTrue(Answers){ var numOfTrue = 0; jQuery.each( Answers, function(i, value) { if (value === "true") numOfTrue++; }); return numOfTrue; } 

Here is jsfiddle if you want to play with it: http://jsfiddle.net/HHrwc/

0
source share
 arr = [true,true,true,false,false] n = arr.join('').split("true").length-1 
0
source share

All Articles