I am currently developing a tool for staff/shift assignments, where each week the staff will be assigned tasks. For the following week, I want to shift all the staff down by one task on the list, with the last staff member cycling back to the top of the list.
The main challenge I'm facing is ensuring that any blank cells in the array remain in their positions while the staff members cycle around them.
Here is an example of what I am trying to achieve:
https://i.sstatic.net/W6hIJ.png
Although I have managed to make it work, I believe my code could be improved in terms of readability and maintainability. So, I am seeking better options.
Below is a functioning example:
let assignedStaff = [
{periodId: 1, staffName: "Jimbo"},
{periodId: 1, staffName: ""},
{periodId: 1, staffName: "Lucy"},
{periodId: 1, staffName: ""},
{periodId: 1, staffName: "Claire"}];
let newPeriod = [];
//Get only the populated staff
populated = assignedStaff.filter(element => element.staffName !== "");
//Move the last staff member to the front
populated.unshift(populated.pop());
for(let i = 0, assigned = 0; i < assignedStaff.length; i++){
if(assignedStaff[i].staffName !== ""){
newPeriod.push(populated[assigned]);
assigned++
}
else
{
newPeriod.push(assignedStaff[i])
}
}
console.log(newPeriod);
For a more readable output, you can view the results on JSBin. It provides better clarity than the run snippet tool.