计算一个值在数组中出现的次数 - javascript - angular - lodash

时间:2015-10-01 01:52:17

标签: javascript angularjs lodash

我有两个javascript数组

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015", ...]

var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", ...]

如何计算格式中条目中出现 openDates 的次数..

var entriesPerDay = [0, 0, 10, 2, 16, 18, 20, ...]

我安装了lodash,但无法弄明白。如果没有匹配,我需要返回0值。

5 个答案:

答案 0 :(得分:1)

到目前为止我能想到什么。

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"]

var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"]

var entriesPerDay = [];

for(var i = 0; i < openDates.length; i++) {
 var currDate = openDates[i];
 var temp = _.filter(entries, function(date) {
   return date === currDate;   
 });
 entriesPerDay.push(temp.length);
}

对于上面的例子,entriesPerDay是这样的:[0,0,2,0,42,0]

PS:我正在使用lodash,正如你所说的那样,你有lodash。

PPS:如果有效,请接受并支持。

答案 1 :(得分:1)

以下示例将帮助您开始并了解如何实现这一目标。

在此示例中,forEach正在数组上用于比较值。

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"]
var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"]

function countEntries(arr1, arr2) {
    var count, total = [];
    arr1.forEach(function(date) {
        count = 0;
        arr2.forEach(function(entry) {
            count += date == entry ? 1 : 0;
        });
        total.push(count);
    });
    return total;
}

console.log(countEntries(openDates, entries));

这里我们只是运行一个forEach循环并重置每次迭代的计数。请参阅以下输出:

输出:[0, 0, 2, 0, 42, 0]

答案 2 :(得分:0)

对于O(n)解决方案而不是(On ^ 2):

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"];
var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"];

function countEntries(open, existing) {
  var openHash = {};
  var entriesHash = {};

  for(var i = 0; i < open.length; i ++){
    openHash[open[i]] = 0;
  }

  for(i = 0; i < existing.length; i ++){
    if(entriesHash.hasOwnProperty(entries[i])){
      entriesHash[entries[i]] ++;
    }else{
      entriesHash[entries[i]] = 1;
    }    
  }

  var result = [];
  for(var date in openHash){
    if(openHash.hasOwnProperty(date)){
      if(entriesHash[date]){
        openHash[date] = entriesHash[date];
      }
      result.push(openHash[date]);
    }
  }

  return result;
}

console.log(countEntries(openDates, entries));

http://jsbin.com/jinolapuza/edit?js,console

答案 3 :(得分:0)

您可以使用lodash的.countBy() .at()来完成此操作。您可以使用_.pick()而不是_.at()来获取具有找到的条目(至少出现1次)的对象作为属性名称。

复杂性 - O(n)。

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"];

var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"];

var counts = _(entries)
            .countBy(function(entry) { return entry }) // count the number of occurances of each entry
            .at(openDates) // get the values of the keys that appear in openDates
            .map(function(count) { return count || 0 }) // map undefined (not found) t0 0
            .value(); // get the values

var countsObject = _(entries)
            .countBy(function(entry) { return entry }) // count the number of occurances of each entry
            .pick(openDates) // get the values of the keys that appear in openDates
            .value(); // get the values

document.write('_.at(): ' + JSON.stringify(counts));
document.write('<br />');
document.write('_.pick(): ' + JSON.stringify(countsObject));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

答案 4 :(得分:-1)

使用map()filter()

_.map(openDates, function(item) {
    return _.filter(entries, function(entry) {
        return item === entry;
    }).length;
});
相关问题