0

Following are code snippet::

  let dayOfWeek=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
  let officeHoursList: [
{dayOfWeek: "Wednesday", beginHour: "08:30 AM", endHour: "04:30 PM"}]

Here I need to compare both of the array and if the dayOfWeek is not present into the officeHoursList then it will add remaining dayOfWeek into the officeHoursList.

For Example in this case the final output would be

officeHoursList: [
{dayOfWeek: "Monday", beginHour: "00:00 AM", endHour: "00:00 PM"},
{dayOfWeek: "Tuesday", beginHour: "00:00 AM", endHour: "00:00 PM"},
{dayOfWeek: "Wednesday", beginHour: "08:30 AM", endHour: "04:30 PM"},
.
.
.
{dayOfWeek: "Sunday", beginHour: "00:00 AM", endHour: "00:00 PM"}]

Here the value of array officeHoursList is dynamic in this case it is only contains the Wednesday but it would be any dynamic value from Monday to Sunday.

Thanks in Advance

3
  • 2
    Thanks for your question, what have you tried on which you need help with? Commented Aug 22, 2019 at 15:19
  • So if nothing is passed you want a 12 hour default with values of beginHour: "00:00 AM", endHour: "00:00 PM"? Please show the code that you have attempted so that we may help you fix it. Commented Aug 22, 2019 at 15:21
  • "Here I need to compare both of the array" how are you comparing? "if the dayOfWeek is not present into the officeHoursList" Are you looking for the current day of the week from the officeHoursList? Commented Aug 22, 2019 at 15:29

4 Answers 4

1

Using reduce() and filter() methods

let dayOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

let officeHoursList = [{dayOfWeek: "Wednesday", beginHour: "08:30 AM", endHour: "04:30 PM"}]

let result = dayOfWeek.reduce((acc, day) => {

  let officeHour = officeHoursList.filter(officeHour => officeHour.dayOfWeek === day)
  
  if (officeHour.length) {
    acc.push(officeHour)
  } else {
    acc.push({dayOfWeek: day, beginHour: "00:00 AM", endHour: "00:00 PM"})
  }
  
  return acc;
}, [])

console.log(result)

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

Comments

0

You can cycle on the day array and check if the current day is not already present, then add it to the office array

dayOfWeek.forEach(function(d){
    if(!officeHoursList.some(function(o){return o.dayOfWeek === d;}){
        officeHoursList.push({
                      dayOfWeek: d, 
                      beginHour: "00:00 AM", 
                      endHour: "00:00 PM"});
    }
});

Comments

0

This example does not do sorting in desired order which I would leave it upto you (OP) to figure it out.

let dayOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
let officeHoursList = [{
  dayOfWeek: "Wednesday",
  beginHour: "08:30 AM",
  endHour: "04:30 PM"
}];

dayOfWeek.map(day => {
  if (!officeHoursList.find(officeHour => officeHour.dayOfWeek === day)) {
    officeHoursList.push({
      dayOfWeek: day,
      beginHour: "00:00 AM",
      endHour: "00:00 PM"
    });
  }
});

console.log(officeHoursList)

2 Comments

Thanks for the reply,But I want the dayOfWeek,In this case Wednesday should be added on 3rd index.
@Pintu that's what I have in my statement above as you need to resolve sorting on your own
0

Ordered list of days and times. If this helps you mark it as accepted answer.

function displayDaysInOfficeHours() {
  console.log(
    officeHoursList.map(
      function(officeHour){
        return officeHour.dayOfWeek+','+officeHour.beginHour+','+officeHour.endHour
      }
    )
  );
}

function addNewDaysOfWeek() {
  displayDaysInOfficeHours(); // Show what we start with

  const orderedOfficeHours = [];

  dayOfWeek.forEach(function(dayOfWeek){ // e.g. 'Monday'
    //Q. Is Monday in the list?
    //If I find it I'll keep track of start and end time
    let startAndEndTime = ['00:00 AM','00:00 PM'];
    for(let i=0; i<officeHoursList.length; i++){
      if (officeHoursList[i].dayOfWeek===dayOfWeek) {
        startAndEndTime= [officeHoursList[i].beginHour,officeHoursList[i].endHour];
        break; // Leave loop early if day found
      }
    }

    orderedOfficeHours.push(
      {
        dayOfWeek: dayOfWeek,
        beginHour: startAndEndTime[0],
        endHour: startAndEndTime[1]
      }
    )
  });

  officeHoursList = orderedOfficeHours;

  displayDaysInOfficeHours(); // Shows ordered officeHours
}

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.