Reputation: 1660
I have an array in jQuery, and I need to count the number of "true" strings in that array, and then make the "numOfTrue" variable equal the number of true strings. So in the below array, there are 2 "true" strings, so numOfTrue would be equal to 2.
var numOfTrue;
var Answers = [ "true", "false", "false", "true", "false" ];
I'm not sure how to do a loop through the array in jQuery to count the strings. Or is a loop even necessary?
The number of true strings could change from anywhere between 1 to 5.
Upvotes: 25
Views: 70961
Reputation: 136074
Using a basic, old-fashioned loop:
var numOfTrue = 0;
for(var i=0;i<Answers.length;i++){
if(Answers[i] === "true")
numOfTrue++;
}
or, a reduce
var numOfTrue = Answers.reduce((acc,curr) => {
if(curr === "true")
acc++;
return acc;
},0);
or a filter
var numOfTrue = Answers.filter(x => x === "true").length;
Upvotes: 66
Reputation: 111
var Answers = [ "true", "false", "not sure", "false", "true", "false", "not sure", "I don't know" ];
function maxDuplicateCountInstances(_arr){
const temp={};
const uniqueArrayKeyToCompare = Array.from(new Set(_arr));
let compareKey='';
console.log('Individually Count');
uniqueArrayKeyToCompare.forEach((unqKey)=>{
const filterOutArray = _arr.filter((key)=>key===unqKey);
temp.name = unqKey;
temp.count = filterOutArray.length;
console.log(temp.name,temp.count);
});
}
maxDuplicateCountInstances(Answers);
Upvotes: 0
Reputation: 1
If you're looking for counting more varied array I think you can implement your code as follow:
var numOfTrue;
var i=0;
var Answers = [ "true", "false", "not sure", "false", "true", "false", "not sure", "I don't know" ];
var counted = [];
var result = {};
Answers.forEach(answer => {// refer to each item in this array with the parameter "answer"
if(!counted.includes(answer)){ // check if answer is not in counted array
counted.push(answer); // add the answer to counted [array]
result[answer] = 1; // add answer to result{object} as a key with a value of 1
}else if(counted.includes(answer)){// here we check if answer is in counted [array]
// if it's true, that means we have already set its value in the result{object} to 1
result[answer] += 1 // now, we just need to increment its value by 1
}
})
console.log(result); // {true: 2, false: 3, not sure: 2, I don't know: 1}
numOfTrue = result.true;
console.log(numOfTrue);// 2
Upvotes: 0
Reputation: 19070
You can also use regular expressions and the String.prototype.match()
Code:
const Answers = [ "true", "false", "false", "true", "false" ];
const count = Answers.toString().match(/true/g).length;
console.log(count)
Upvotes: 2
Reputation: 929
Using only JS
function Count(targetString){
var vowels = ['a','o','u','i','e'];
return targetString.split("").reduce(function(countingVaues,char){
countingVaues +=
vowels.filter(function(x){return x === char;}).length;
return countingVaues;
},0)};
Count("aouiesndfjksdnjkfnsdjkewwe"); //==7
Upvotes: 0
Reputation: 28812
You don't need jQuery for this task. Just plain Javascript. The best answer is given by Jamiec by you could also use filter or reduce functions that are available for arrays.
filter function:
var numOfTrue = Answers.filter(function(item){ return item === "true"; }).length
Here you are providing callback function that is executed for every item in array. If this function returns true
then this item will be in returned array. Then you get the length of this array ( length of ["true", "true"]
array is 2
)
reduce function:
var numOfTrue = Answers.reduce(function(counter, item){ return counter + (item === "true" ? 1 : 0); }, 0);
Reduce function need callback function that is executed for every item in array. Array item is provided as second argument, the first one is the return value of previously executed callback. The second argument of reduce function is initial value that is provided for first callback function. If you omit this, then the first item from array is provided (in this example, if you forget to add 0
as second argument then the result value will be "true0010"
string, because instead of adding numbers you will be concatenating stings)
Upvotes: 7
Reputation: 21
arr = [true,true,true,false,false]
n = arr.join('').split("true").length-1
Upvotes: 1
Reputation: 69905
var numOfTrue = 0;
for(var i = 0, len = Answers.length;i < len; i++){
if(Answers[i] === "true"){
numOfTrue++;
}
}
Upvotes: 1
Reputation: 79830
You don't need jQuery for this.. a simple for loop like below would 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 loop, DEMO
Answers.toString().match(/true/g).length
Upvotes: 12
Reputation: 330
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/
Upvotes: 1
Reputation: 94429
var numOfTrue =0;
$.each(Answers,function(index,value){
if(value =="true")
{
numOfTrue++;
}
});
Upvotes: 1
Reputation: 134167
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 a jsfiddle if you want to play around with it: http://jsfiddle.net/HHrwc/
Upvotes: 0
Reputation: 30095
It might be not so performance friendly but you can use filtering and then count using grep:
var num = jQuery.grep(Answers, function (a) { return a == "true"; }).length;
Upvotes: 7
Reputation: 337560
If you did want to use jQuery (and tbh, you shouldn't for this) this will work:
var numOfTrue;
var Answers = [ "true", "false", "false", "true", "false" ];
$.each(Answers, function(i, item) {
if (item == "true")
numOfTrue++;
});
The Javascript equivalent is:
var numOfTrue;
var Answers = [ "true", "false", "false", "true", "false" ];
for (var i = 0; i < Answers.length; i++) {
if (answers[i] == "true")
numOfTrue++;
});
Upvotes: 1