2017-05-23 31 views
0

我不想在標題中有目的地模糊,我很抱歉。只是無法弄清楚解決這個問題的一個好的和簡潔的方法。解決複雜數組連接和分析的最優雅方法?

我正在構建一個應用程序,爲一組個人顯示他們在滾動28天內的位置。在視覺上(我不知道這一點),我們基本上是在討論一張桌子上最左邊一列的人物,28天頂部和表格單元格中的下落。

源數據由兩個列表構成(在SharePoint中)。在其中一個,他們記錄他們的「默認」位置。另一方面,他們表明他們將要在何時何地旅行。在任何情況下,我得到過的對象的數組是這樣的:

從下落列表:

[ 
    {Date:'2017-05-25T00:00:00Z',EventLength:'Full Day',TeamMem:'Person McPerson',Title:'Public Holiday'}, 
    //Lots more. 
] 

從默認位置列表:

[ 
    {TeamMem:'Person McPerson',Location:'Main Office'} 
] 

我要弄清楚如何將所有這些結合在一起,這樣,對於每個人,我都有一組對象,在今天和今天+ 28天之間的任何一天給出該人的位置。有趣的一面是,他們插入行蹤清單的項目可能是全天或半天(PM或AM)。所以我也需要在這次合併中考慮半天。

This is how I've solved it.

TL; DR,我做的28天的陣列,使用下劃線的_.difference()函數查找的日子裏,每個團隊成員,那裏沒有下落項中添加的項背到指定默認位置的每個日期的行蹤陣列,然後做一件我幾乎可以跟隨的事情,我一年前處理半天的項目。

它的工作原理,但我知道這可能不是最優雅的方式來處理這個問題。有人有主意嗎?

編輯爲清楚起見

最後,我必須每人一個數組,一個爲我追蹤行蹤每天28個項目。在28天的時間段內,在今天和今天+28天之間,我應該有一些行蹤信息和一些默認位置,具體取決於團隊成員實際登錄系統的情況。

多個編輯

我可以對這個被說服,但我想最終的陣列需要看起來像這樣:

[ 
    Person1 McPerson:[//Array of 28 events, either their whereabouts or defaults.], 
    Person2 McPerson:[//Array of 28 events, either their whereabouts or defaults.], 
    Person3 McPerson:[//Array of 28 events, either their whereabouts or default locations.], 
    //Etc, for however many people. 
] 

編輯額外的例子

希望這些澄清一點:

回答

0

我不能完全肯定,如果我知道你想用什麼樣的日期。它可以幫助,如果您有:

  • 更大的陣列開始,與得到過濾掉這兩個項目,並且留
  • 項目的結果數組應該像

這裏什麼是我的回答如此遠:

// POLYFILL FOR Array.prototype.find 

// https://tc39.github.io/ecma262/#sec-array.prototype.find 
if (!Array.prototype.find) { 
    Object.defineProperty(Array.prototype, 'find', { 
    value: function(predicate) { 
    // 1. Let O be ? ToObject(this value). 
     if (this == null) { 
     throw new TypeError('"this" is null or not defined'); 
     } 

     var o = Object(this); 

     // 2. Let len be ? ToLength(? Get(O, "length")). 
     var len = o.length >>> 0; 

     // 3. If IsCallable(predicate) is false, throw a TypeError exception. 
     if (typeof predicate !== 'function') { 
     throw new TypeError('predicate must be a function'); 
     } 

     // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. 
     var thisArg = arguments[1]; 

     // 5. Let k be 0. 
     var k = 0; 

     // 6. Repeat, while k < len 
     while (k < len) { 
     // a. Let Pk be ! ToString(k). 
     // b. Let kValue be ? Get(O, Pk). 
     // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). 
     // d. If testResult is true, return kValue. 
     var kValue = o[k]; 
     if (predicate.call(thisArg, kValue, k, o)) { 
      return kValue; 
     } 
     // e. Increase k by 1. 
     k++; 
     } 

     // 7. Return undefined. 
     return undefined; 
    } 
    }); 
} 

// END OF POLYFILL 



var whereabouts = [ 
    {Date:'2017-05-25T00:00:00Z',EventLength:'Full Day',TeamMem:'Person McPerson',Title:'Public Holiday'}, 
]; 

var defaultLocations = [ 
    {TeamMem:'Person McPerson',Location:'Main Office'} 
]; 


var result = whereabouts 
    .filter(function(el) { return new Date(el.Date) > new Date() && new Date(el.Date) < new Date(new Date().setDate(new Date().getDate() + 28))}) 
    .map(function(el, i) { 
    el.Location = defaultLocations 
     .find(function(lo) { return lo.TeamMem === el.TeamMem; }) 
     .Location; 
    return el; 
    }); 

console.log(result) 
+0

嘿,對不起,如果這不明確。我可能不需要日期。我使用日期來確定我是否有那天的下落。 – ChrisPEditor

+0

我明白了。我稍微改變了代碼,因爲我假定數組的大小和相應的索引是相同的,這不一定是真的。請讓我知道這是否適合你。 – Sventies

+0

嗯。我不能在普通數組上使用.find()。我將它轉換爲.filter(因爲我們只是過濾當前團隊成員的默認值),但是這仍然只是從行蹤列表中返回原來的10個左右的行蹤。 – ChrisPEditor

相關問題