I have an array that I put together in jQuery and I'm wondering if there is a way that I could find the number of occurrences of a given term. Would I have better results if I tried creating a string instead?
If you have an array like this:
var arr = [1, 2, 3, 4, 3, 2, 1];
And set your target value:
var target = 1;
Then you can find the number of occurences of target
using:
var numOccurences = $.grep(arr, function (elem) {
return elem === target;
}).length; // Returns 2
You can probably do like this -
var myArray = ['a','fgh','dde','a3e','rra','ab','a'];
var occurance = 0;
var lookupVal = 'a';
$(myArray).each(function (index, value) {
if(value.indexOf(lookupVal)!= -1)
{
occurance++;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With