2017-12-18 450 views
-3

我有一個數組,我要計數和排序。我怎樣才能做到這一點?我的嘗試會導致無限循環。排序並將空值添加到數組如果需要

我想這個數組

[ 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-05", timestamp: "2017-12-05 15:58:25", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-06", timestamp: "2017-12-06 10:13:12", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-06", timestamp: "2017-12-07 09:38:12", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-08", timestamp: "2017-12-08 09:35:25", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-11", timestamp: "2017-12-11 11:25:31", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-12", timestamp: "2017-12-12 15:05:17", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-13", timestamp: "2017-12-13 11:08:40", …} 
{activityId: 201609011024550000, Employee_employeeId: 100, projectId: 300000000000000200, Date_date: "2017-12-15", timestamp: "2017-12-15 11:51:33", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-18", timestamp: "2017-12-18 11:04:56", …} 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-15", timestamp: "2017-12-18 12:04:56", …} 
] 

看起來像基於此陣/排序上Date_date

(Date_date: 「2017年12月1日」 不存在,所以值是0, Date_date:「2017-12-06」存在兩次,因此值爲2)

[0,0,0,0...,1,2,0,1...] 

好的,如果新建數組已創建。沒有jquery。

編輯: 對不起,這裏是循環不停止

for(var i = 0; i < a.length; i++){ 
    for(var j = 0; j <= b.length; j++){ 
     if(parseInt(a[i].Date_date.substring(8))-1==j){ 
      var num = b[j]; 
      b[j] = num+1; 
     } 
     else{ 
      var num = b[j]; 
      b[j] = num+0; 
     } 
    } 
} 
+0

請更新這個職位與你已經嘗試過的代碼, – 31piy

+0

添加您嘗試過的代碼,而不是直接要求的解決方案。另外,添加日期範圍。 –

+0

看來你的代碼有問題。但是,除非我們有[可以重現問題的代碼或信息](// stackoverflow.com/help/mcve),否則我們無能爲力。否則,我們只是盲目猜測。 – Cerbrus

回答

2

首先排序陣列上日期,然後創建一個對象,查找基於Date_date。然後從排序後的數組中獲取第一個和最後一個日期,併爲該月的第一天和該月的最後一天創建日期對象。然後使用while循環,通過檢查對象查找添加記錄直到最後一個日期。

var data = [{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-05", timestamp: "2017-12-05 15:58:25"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-06", timestamp: "2017-12-06 10:13:12"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-06", timestamp: "2017-12-07 09:38:12"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-08", timestamp: "2017-12-08 09:35:25"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-11", timestamp: "2017-12-11 11:25:31"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-12", timestamp: "2017-12-12 15:05:17"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-13", timestamp: "2017-12-13 11:08:40"},{activityId: 201609011024550000, Employee_employeeId: 100, projectId: 300000000000000200, Date_date: "2017-12-15", timestamp: "2017-12-15 11:51:33"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-18", timestamp: "2017-12-18 11:04:56"},{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-15", timestamp: "2017-12-18 12:04:56"},]; 
 

 
var dateCount = data 
 
    .sort((a,b) => new Date(a.Date_date) - new Date(b.Date_date)) 
 
    .reduce((r, o) => { 
 
     r[o.Date_date] = r[o.Date_date] || []; 
 
     r[o.Date_date].push(o) 
 
     return r; 
 
    }, {}); 
 

 
//Get the first date from the sorted array 
 
var date1 = new Date(data[0].Date_date); 
 
var firstDay = new Date(date1.getFullYear(), date1.getMonth(), 2); 
 

 
//Get the last date from the sorted array 
 
var date2 = new Date(data[data.length - 1].Date_date); 
 
var lastDay = new Date(date2.getFullYear(), date2.getMonth() + 1, 1); 
 

 
var result = []; 
 
while(firstDay <= lastDay){ 
 
    var count = (dateCount[firstDay.toISOString().split('T')[0]] || []).length; 
 
    result.push(count); 
 
    firstDay.setDate(firstDay.getDate() + 1); 
 
} 
 

 
console.log(result);

+1

非常感謝! – Softy

+0

哈桑,看起來00天正在取代第31天。陣列長度爲31,但第5天在[5],應該在pos [4]。我怎樣才能解決這個問題?我會提出一個新問題嗎? – Softy

+0

更新瞭解決方案,請現在檢查。 –

1

首先我試過的代碼,你需要得到每個日期出現在一個陣列的次數。所以我使用data.forEach迭代了數組中的每個對象。

請注意,我正在跟蹤名爲counter的對象中的日期計數。

然後,我使用data.map將陣列中的每個對象映射到新陣列中的項目。因此,對於data陣列中的每個項目,我都會返回觀察次數。

如果你不熟悉Array#mapArray#forEach,你可以閱讀更多。

var data = [ 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-05", timestamp: "2017-12-05 15:58:25"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-06", timestamp: "2017-12-06 10:13:12"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-06", timestamp: "2017-12-07 09:38:12"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-08", timestamp: "2017-12-08 09:35:25"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-11", timestamp: "2017-12-11 11:25:31"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-12", timestamp: "2017-12-12 15:05:17"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-13", timestamp: "2017-12-13 11:08:40"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 100, projectId: 300000000000000200, Date_date: "2017-12-15", timestamp: "2017-12-15 11:51:33"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-18", timestamp: "2017-12-18 11:04:56"}, 
 
{activityId: 201609011024550000, Employee_employeeId: 4075, projectId: 300000000000000200, Date_date: "2017-12-15", timestamp: "2017-12-18 12:04:56"} 
 
]; 
 

 
var counter = {}; 
 

 
data.forEach(function(item) { 
 
    var date = item.Date_date; 
 
    
 
    if(!!counter[date]) { 
 
    counter[date]++; 
 
    } else { 
 
    counter[date] = 1; 
 
    } 
 
}); 
 

 
console.log("Date-wise counts: ", counter); 
 

 
var result = data.map(function(item) { 
 
    return counter[item.Date_date]; 
 
}); 
 

 
console.log("Result:", result);

+0

嗨Nisarg和謝謝你的答案。這看起來在正確的道路上,我仍然需要添加失蹤日期aswell(「2017-12-01」:0,)。我是否創建一個新的循環並使用計數器創建一個新的數組? – Softy

+0

@Softy是的,類似的東西。我錯過了這個問題中的那一部分。你想讓我展示一下嗎? – Nisarg

+0

沒問題,我會用哈桑提供的答案。您的回答仍將由我維持(我的聲望在15以下,因此投票將在稍後開始) – Softy

相關問題