1

I am trying to sum up each member's total absent days.

Let's have only two members: A and B. A had left two times for different reasons, she went holiday off for 3 days and some other reason for 2 days. B had been hospitalized for 8 days, went vacation for 4 days.

So, here is the code I did my best.

*The dates I wrote is for example. Date values will be posted by JS onclick. No need of toString or something else.

var member = [{
  name : "A",
  absent : [{
    reason : "Leave",
    start_date : "2020-02-01",
    end_date : "2020-02-03"
  },{
    reason : "etc",
    start_date : "2020-02-15",
    end_date : "2020-02-16"
  }
  ]
},{
  name : "B",
  absent : [{
    reason : "Hospitalized",
    start_date : "2020-02-03",
    end_date : "2020-02-10"
  },{
    reason : "Leave",
    start_date : "2020-02-10",
    end_date : "2020-02-13"
  }
  ]
}]

var countAbsentDays = function(){
  var countDays_start = member[0].absent[0].start_date;
  var countDays_end = member[0].absent[0].end_date;
  var diff = (new Date(countDays_end)) - (new Date(countDays_start));
  var count = diff/(1000 * 60 * 60 * 24);
  
  return count;
};

console.log(countabsentDays());

To collect each member's total absent days, I need to develop the code below using for-loop.

 var countDays_start = member[0].absent[0].start_date;

Guess, the index number I put 0 could be replaced with variable. But I can't picture how this function runs all over absent array and its element absent.start_date and absent.end_date.

Anybody help?

1 Answer 1

1

You can use map to transform an array, and reduce to accumulate the array elements into a value.

    var member = [{
      name : "A",
      absent : [{
        reason : "Leave",
        start_date : "2020-02-01",
        end_date : "2020-02-03"
      },{
        reason : "etc",
        start_date : "2020-02-15",
        end_date : "2020-02-16"
      }
      ]
    },{
      name : "B",
      absent : [{
        reason : "Hospitalized",
        start_date : "2020-02-03",
        end_date : "2020-02-10"
      },{
        reason : "Leave",
        start_date : "2020-02-10",
        end_date : "2020-02-13"
      }
      ]
    }]

    var countAbsentDays = function(){
      return member.map( m => // map every element of member array to object { [m.name]: (reduced value of absent array) }
          ({
             [m.name]: 
                m.absent.reduce(
                    (acc, x) => 
                      acc + dateDiff(x.end_date, x.start_date)
                , 0 )  // accumulator + dateDiff result returned for every element
           })
      );
      function dateDiff(end, start) {
        var countDays_start = start;
        var countDays_end = end;
        var diff = (new Date(countDays_end)) - (new Date(countDays_start));
        var count = diff/(1000 * 60 * 60 * 24);

        return count;
      }
    };

    console.log(countAbsentDays());

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.