有什么方法可以计算jQuery数组中的出现次数? [英] Is there any way to count the number of occurrences in a jQuery array?
本文介绍了有什么方法可以计算jQuery数组中的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在jQuery中有一个数组,我想知道是否有一种方法可以找到给定术语的出现次数.如果我尝试创建一个字符串,是否会有更好的结果?
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?
推荐答案
如果您有这样的数组:
var arr = [1, 2, 3, 4, 3, 2, 1];
并设置您的目标值:
var target = 1;
然后您可以使用以下命令找到target
的出现次数:
Then you can find the number of occurences of target
using:
var numOccurences = $.grep(arr, function (elem) {
return elem === target;
}).length; // Returns 2
这篇关于有什么方法可以计算jQuery数组中的出现次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文