2015-10-01 58 views
1

我有兩個JavaScript數組計算價值多少次發生在陣列 - 的JavaScript - 角 - lodash

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值。

+0

拿一個項目在'openDates',它與每個項目在'entries'相比,增加了計數,如果匹配。它有什麼困難嗎? – AnhTriet

回答

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:如果它有效,請接受並注意。

+0

你真的想要這個答案大聲笑。我應該刪除我的嗎? – Jesse

+0

@Jesse你爲什麼這麼說?我不明白你是什麼意思..但是爲什麼要刪除你的答案?這只是我用過lodash,你沒有任何圖書館。人們可以通過兩種方式瞭解它不是很好嗎? – Surya

+0

你很好蘇里亞。 – Jesse

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]

0

對於O(n)的解決方案,而不是(在^ 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

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>

-1

使用map()filter()

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