繁体   English   中英

计算值在数组中出现的次数-Javascript-angular-lodash

[英]Count how many times a value occurs in an array - javascript - angular - 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值。

到目前为止我能想到的。

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:请接受并支持(如果可行)。

下面的示例将帮助您入门,并获得有关如何完成此操作的想法。

在此示例中,在数组上使用了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]

对于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

您可以使用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> 

使用map()filter()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM