排序并将空值添加到数组如果需要

我有一个数组,我要计数和排序。我怎样才能做到这一点?我的尝试会导致无限循环。排序并将空值添加到数组如果需要

我想这个数组

[ 

{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;

}

}

}

回答:

首先排序阵列上日期,然后创建一个对象,查找基于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);

回答:

首先我试过的代码,你需要得到每个日期出现在一个阵列的次数。所以我使用data.forEach迭代了数组中的每个对象。

请注意,我正在跟踪名为counter的对象中的日期计数。

然后,我使用data.map将阵列中的每个对象映射到新阵列中的项目。因此,对于data阵列中的每个项目,我都会返回观察次数。

如果你不熟悉Array#map和Array#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);

以上是 排序并将空值添加到数组如果需要 的全部内容, 来源链接: utcz.com/qa/263839.html

回到顶部