1

I have two hashmaps which both have a string as a key and an array of strings as a value. I need to modify one of the values in one HashMap based on the values in the other.

So basically I have an HashMap called genericSlots and another called bookedSlots. If the bookedSlots HashMap values are also contained in the genericSlots values I would like those values to be removed from the genericSlots HashMap

I have tried doing this as shown in the code below but the values in the genericSlotsAsArray are not being removed.

Here is the code:

  checkForBookedSlots(date:string){

  let genericSlots = new Map<string,string[]>();
  let bookedSlots = new Map<string,string[]>();
  
  let bookedArray = ['09:00','11:00']
  let slotTimes = ["09:00", "09:30","10:00", "10:30", "11:00", "11:30", "12:00", "12:30","13:00","13:30", "14:00","14:30","15:00","15:30","16:00","16:30"]
  
  bookedSlots.set("09/10/2021",bookedArray)
  genericSlots.set(date,slotTimes)
 
  let genericSlotsAsArray = Array.from(genericSlots.values());
  let bookedSlotsAsArray = Array.from(bookedSlots.values())
  
  for(let i=0;i< genericSlotsAsArray.length; i++){
      for(let j=0;j<bookedSlotsAsArray.length;j++){
          let mValue = bookedSlotsAsArray[j];
          if(mValue === genericSlotsAsArray[i]){
              const index = genericSlotsAsArray.indexOf(mValue, 0)
              genericSlotsAsArray.splice(index, 1)
          }

      }
  }
  console.log('Generic Slots -> ', genericSlotsAsArray)
  console.log('Booked Slots -> ',  bookedSlotsAsArray) }

Expected output:

Generic Slots - > ["09:30","10:00", "10:30","11:30", "12:00", "12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30"]

Booked Slots -> ["09:00","11:00"] 

 

Actual Output:

Generic Slots - > ["09:00","09:30","10:00", "10:30","11:00","11:30", "12:00", "12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30"]

Booked Slots -> ["09:00","11:00"]

1 Answer 1

1

You can do something like this

checkForBookedSlots(date: string) {
    let genericSlots = new Map<string, string[]>();
    let bookedSlots = new Map<string, string[]>();

    let bookedArray: string[] = ['09:00', '11:00'];
    let slotTimes: string[] = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30'];
    let genericSlotsArray: string[] = slotTimes.filter(slot => !bookedArray.includes(slot));

    genericSlots.set(date, genericSlotsArray);
    bookedSlots.set(date, bookedArray);

    console.log('Generic Slots -> ', genericSlotsArray);
    console.log('Booked Slots -> ', bookedArray);
}
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.