【问题标题】:check count of items in Array using jquery [duplicate]使用jquery检查数组中的项目计数[重复]
【发布时间】:2017-10-01 03:35:02
【问题描述】:

我正在关注此Counting the occurrences / frequency of array elements 以查找该事件 数组中的每一项。 如果任何项目出现超过或等于 3 次,那么我必须显示警报。

下面是我试过的代码:

var countries = ['India', 'Australia', 'Bangladesh', 'India', 'India',
  'New Zealand', 'New Zealand', 'Pakistan', 'Pakistan', 'West Indies'];

var recipientsArray = countries.sort();

var reportRecipientsDuplicate = [];
for (var i = 0; i < recipientsArray.length; ++i) {
  if (!reportRecipientsDuplicate[recipientsArray[i]]) {
    reportRecipientsDuplicate[recipientsArray[i]] = 0;
  }
  ++reportRecipientsDuplicate[recipientsArray[i]];
}

如何检查某个项目是否出现超过或等于 3 次?谢谢

【问题讨论】:

  • var countries = ....
  • 只需循环遍历数组reportRecipientsDuplicate 并在遇到大于或等于 3 的数字时提醒消息。
  • 数组使用(方括号)[]定义。请编辑您的代码。

标签: javascript jquery arrays function


【解决方案1】:

const array = ['India' , 'Australia' , 'Bangladesh' , 'India' , 'India' , 'New Zealand' , 'New Zealand' , 'Pakistan' , 'Pakistan' , 'West Indies'];


_.each(_.uniq(_.filter(array, function(v) {
    return _.filter(array, function(v1) {
        return v1 === v;
    }).length > 2;
})), function(i) {
    console.log(i);
});
  
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"&gt;&lt;/script&gt;

使用 UnderscoreJS:

_.uniq(_.filter(array, v => 
  _.filter(array, v1 => v1 === v).length > 2));

代码

_.each(_.uniq(_.filter(array, function(v) {
    return _.filter(array, function(v1) {
        return v1 === v;
    }).length > 2;
})), function(i) {
    console.log(i);
});

演示 https://jsfiddle.net/sumitridhal/e9yf3txg/3/

【讨论】:

    【解决方案2】:

    试试这个代码:

    var countries = ["India" , "Australia" , "Bangladesh" , "India" , "India" , "New Zealand" , "New Zealand" , "Pakistan" , "Pakistan" , "West Indies","New Zealand"];
    
    var recipientsArray = countries.sort(); 
    checkedWords = [];
    
    for (var i = 0; i < recipientsArray.length; ++i) {
        var count = 0;
    
      for(var j=i;j<recipientsArray.length;j++){
        if(checkedWords.indexOf(recipientsArray[i]) > -1){
            continue;
        }
        if(recipientsArray[i] == recipientsArray[j]){
            count = count+1;
        }
      }
      console.log(count);
      if(count >= 3){
        alert(recipientsArray[i]);
      }
      checkedWords.push(recipientsArray[i]);
    }
    

    https://jsfiddle.net/74bzzze3/

    我希望这能回答你的问题:)

    【讨论】:

      【解决方案3】:

      您可以使用reduce 方法轻松解决此问题。 reduce 方法通过应用提供的回调函数创建一个新的数组或对象。

      reduce() 方法对累加器应用一个函数,并且 数组中的每个元素(从左到右)将其缩减为 单值。

      var countries = ['India' , 'Australia' , 'Bangladesh' , 'India' , 'India' , 'New Zealand' , 'New Zealand' , 'Pakistan' , 'Pakistan' , 'West Indies'];
      var statistics=countries.reduce(function(obj,elem){
          obj[elem]=obj[elem] || 0;
          obj[elem]++;
          return obj;
      },{});
      for(country in statistics){
        if(statistics[country]>=3)
          console.log(country);
      } 

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        • 2021-08-07
        • 2020-11-21
        • 1970-01-01
        相关资源
        最近更新 更多