优化 JS Function 按月份过滤数据
Optimize the JS Function to filter the data based on Month
目前,我已经编写了下面的函数来根据数组中的月份和 return 过滤项目,并且我在下面使用了登录。
有人可以帮助我使用 datejs 或 moment 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;
});
}
您可以使用Underscore group by with momentjs类似
的东西
var data= _.groupBy(arr,function(item){return moment(inputs.executedOn,"DD/MMM/YY").format("MMM")})
var janData=data["Jan"] ? data["Jan"].length : 0;
目前,我已经编写了下面的函数来根据数组中的月份和 return 过滤项目,并且我在下面使用了登录。
有人可以帮助我使用 datejs 或 moment 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;
});
}
您可以使用Underscore group by with momentjs类似
的东西var data= _.groupBy(arr,function(item){return moment(inputs.executedOn,"DD/MMM/YY").format("MMM")})
var janData=data["Jan"] ? data["Jan"].length : 0;