Is there any way to count the number of occurrences in a jQuery array?

前端 未结 4 1465
失恋的感觉
失恋的感觉 2021-01-02 08:48

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 i

4条回答
  •  伪装坚强ぢ
    2021-01-02 09:22

    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++;
         }
    });
    

提交回复
热议问题