jQuery的:由计数值数组元素 [英] jQuery: count array elements by value

查看:132
本文介绍了jQuery的:由计数值数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有组装元素的数组:

  timeArray = [];
$('。time_select')。每个(函数(I,选择){
  timeArray [I] = $(选择).VAL();
});

其中, .time_select 是一组不同的HTML选择标签的类。

我想要做的就是计数 timeArray 在发生特定值的次数。奇怪的是,我还没有发现任何简洁的解决方案......肯定有一个简单的方法来做到这一点?


解决方案

  timeArray = [];
发生= {};
$('。time_select')。每个(函数(I,选择){
  timeArray [I] = $(选择).VAL();
  如果(!出现[timeArray [我] = NULL){出现[timeArray [I]] ++; }
  其他{发生[timeArray [Ⅰ]] = 1; }
});

I have an array of elements assembled by:

timeArray = [];
$('.time_select').each(function(i, selected) {
  timeArray[i] = $(selected).val();
});

where .time_select is a class on a group of different HTML select tags.

What I'd like to do is count the number of times a specific value occurs in timeArray. Strangely enough, I haven't found any concise solution... surely there's a simple way to do this?

解决方案

timeArray = [];
occurs = {};
$('.time_select').each(function(i, selected) {
  timeArray[i] = $(selected).val();
  if (occurs[timeArray[i]] != null ) { occurs[timeArray[i]]++; }
  else {occurs[timeArray[i]] = 1; }
});

这篇关于jQuery的:由计数值数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆