目前,我已经编写了以下函数来过滤基于月份的项目并在Array中返回,我使用了以下登录。
有人能提供帮助是否有任何方法可以更有效地使用datejs或者时刻js使用for循环以更有效的方式过滤
我在这里以2/Jan/17
的格式获取日期并根据includes
方法进行过滤
提前致谢!!
Javascript
function getMonthlyCount() {
return yearlyExecution()
.then(function(myitems) {
monthlyDataForGraph = [];
var janFinalCount = febFinalCount = marFinalCount = aprFinalCount = mayFinalCount = junFinalCount = julFinalCount = augFinalCount = sepFinalCount = octFinalCount = novFinalCount = decFinalCount = 0;
myitems.forEach(function(items) {
releasename = items[0].cycleName;
janData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Jan/17"))
return inputs;
});
febData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Feb/17"))
return inputs;
});
marData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Mar/17"))
return inputs;
});
aprData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Apr/17"))
return inputs;
});
mayData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("May/17"))
return inputs;
});
junData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Jun/17"))
return inputs;
});
julData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Jul/17"))
return inputs;
});
augData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Aug/17"))
return inputs;
});
sepData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Sep/17"))
return inputs;
});
octData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Oct/17"))
return inputs;
});
novData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Nov/17"))
return inputs;
});
decData = $filter('filter')(items, function(inputs) {
if (inputs.executedOn.includes("Dec/17"))
return inputs;
});
janFinalCount = janFinalCount + janData.length;
febFinalCount = febFinalCount + febData.length;
marFinalCount = marFinalCount + marData.length;
aprFinalCount = aprFinalCount + aprData.length;
mayFinalCount = mayFinalCount + mayData.length;
junFinalCount = junFinalCount + junData.length;
julFinalCount = julFinalCount + julData.length;
augFinalCount = augFinalCount + augData.length;
sepFinalCount = sepFinalCount + sepData.length;
octFinalCount = octFinalCount + octData.length;
novFinalCount = novFinalCount + novData.length;
decFinalCount = decFinalCount + decData.length;
});
monthlyDataForGraph = [janFinalCount, febFinalCount, marFinalCount, aprFinalCount, mayFinalCount, junFinalCount, julFinalCount, augFinalCount, sepFinalCount, octFinalCount, novFinalCount, decFinalCount];
console.log('Monthly Data For Graph', monthlyDataForGraph);
return monthlyDataForGraph;
});
}
答案 0 :(得分:1)
您可以Underscore group by使用momentjs之类的内容
var data= _.groupBy(arr,function(item){return moment(inputs.executedOn,"DD/MMM/YY").format("MMM")})
var janData=data["Jan"] ? data["Jan"].length : 0;