Reputation: 4035
I have an array where I have the weekly progress of some projects .
In the cuurent week, timestamp of 1529539200 or date 06/21/2018, I have the information of 3 projects. Project 20, 21 and 22.
In the other week, a week before the current 06/14/2018, there's only information of 2 projects. Because project 22 didn't exist.
progressList = [
// current 06/21/2018
{
project: 20,
timestamp: 1529539200, // seconds
current: true,
progress: 90
},
{
project: 21,
timestamp: 1529539200,
current: true,
progress: 70
},
{
project: 22,
timestamp: 1529539200,
current: true,
progress: 100
},
// 06/14/2018
{
project: 20,
timestamp: 1528934400, // a week before (1529539200 - 604800)
current: false,
progress: 80
},
{
project: 21,
timestamp: 1528934400,
current: false,
progress: 50
}
]
What I need is a list of current projects, but includes a new property "increase", that is equals to the difference between current project progress and the progress of the week before.
For example I should get something like this:
currentProgressList = [
{
project: 20,
timestamp: 1529539200,
current: true,
progress: 90,
increase: 10 // difference
},
{
project: 21,
timestamp: 1529539200,
current: true,
progress: 70,
increase: 20
},
{
project: 22,
timestamp: 1529539200,
current: true,
progress: 100,
increase: 0 //
}
]
I guess that filtering the projects by current
property may be simple:
listadoprogresss.filter(p => {
return p.current === true
})
But, I don't know how to add the increase
property according with my needs.
Thanks in advance
Upvotes: 1
Views: 1303
Reputation: 460
Here is a possible solution, you can adjust the checkIncrease to suit your solution. but run the code snippet
let checkIncrease = function(list, p) {
let prevProgress = list.find(np => np.project === p.project && !np.current)
var increase = 0;
// confirm that its defined
if (prevProgress) increase = p.progress - prevProgress.progress
// create a clone and return
return { project: p.project, timestamp: p.timestamp, current: p.current, progress: p.progress, increase: increase }
}
let getProgressIncrease = function(progressList) {
return progressList
.filter(p => p.current)
.map(p => checkIncrease(progressList, p))
}
var progressList = [
// current 06/21/2018
{
project: 20,
timestamp: 1529539200, // seconds
current: true,
progress: 90
},
{
project: 21,
timestamp: 1529539200,
current: true,
progress: 70
},
{
project: 22,
timestamp: 1529539200,
current: true,
progress: 100
},
// 06/14/2018
{
project: 20,
timestamp: 1528934400, // a week before (1529539200 - 604800)
current: false,
progress: 80
},
{
project: 21,
timestamp: 1528934400,
current: false,
progress: 50
}
]
// try it out
let result = getProgressIncrease(progressList)
console.log(result)
Upvotes: 2
Reputation: 11539
const progressList = [
{ project: 20, timestamp: 1529539200, current: true, progress: 90 },
{ project: 21, timestamp: 1529539200, current: true, progress: 70 },
{ project: 22, timestamp: 1529539200, current: true, progress: 100 },
{ project: 20, timestamp: 1528934400, current: false, progress: 80 },
{ project: 21, timestamp: 1528934400, current: false, progress: 50 }
]
function getCurrentProgressList(progressList) {
const projects = [];
const indexes = {};
for(const Project of progressList) {
const index = indexes[Project.project];
const CurrentProject = projects[index];
if (Project.current) {
indexes[Project.project] = projects.length;
projects.push({ ...Project, increase: 0 });
} else if (index !== undefined) {
const a = CurrentProject.progress;
const b = Project.progress;
CurrentProject['increase'] = a - b;
}
}
return projects;
}
console.log(getCurrentProgressList(progressList));
Upvotes: 2