[英]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));
您可以使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.