findLastKey

findLastKey

  • (predicate: Predicate) => (anArray: array) => number | undefined
  1. Find the last index which passes a condition.

    const lt3 = n => n < 3
    const findLastKeyLt3 = findLastKey(lt3)
    
    findLastKeyLt3([1, 2, 3]) // is 1
    findLastKeyLt3([3, 4, 5]) // is undefined
    const lt3 = (n: number) => n < 3
    const findLastKeyLt3 = findLastKey(lt3)
    
    findLastKeyLt3([1, 2, 3]) // is 1
    findLastKeyLt3([3, 4, 5]) // is undefined
    
    const lt3 = n => n < 3
    const findLastKeyLt3 = findLastKey(lt3)
    
    findLastKeyLt3([1, 2, 3]) // is 1
    findLastKeyLt3([3, 4, 5]) // is undefined
    const lt3 = (n: number) => n < 3
    const findLastKeyLt3 = findLastKey(lt3)
    
    findLastKeyLt3([1, 2, 3]) // is 1
    findLastKeyLt3([3, 4, 5]) // is undefined
    
  2. Sometimes we want to find the last index that passes a condition. Below, we have Liz's schedule for the week. She can only take three appointments per day. Her client asks to schedule an appointment as late in the week as possible. Let's find which day she has open.

    const dayOfWeek = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
    
    const appointmentsByDay = [
      ['9am-10', '11am-12', '1:30pm-3'],
      ['8:30am-10', '1pm-2', '2:30pm-3:30'],
      [],
      ['9:30am-10:30', '2pm-3'],
      ['8am-10', '11am-12', '1pm-3'],
    ]
    
    const hasOpening = appts => appts.length < 3
    const findLastOpening = findLastKey(hasOpening)
    const lastOpeningIdx = findLastOpening(appointmentsByDay)
    const lastOpeningDay = dayOfWeek[lastOpeningIdx]
    
    console.log(lastOpeningDay)
    // is thursday
    const dayOfWeek = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
    
    const appointmentsByDay = [
      ['9am-10', '11am-12', '1:30pm-3'],
      ['8:30am-10', '1pm-2', '2:30pm-3:30'],
      [],
      ['9:30am-10:30', '2pm-3'],
      ['8am-10', '11am-12', '1pm-3'],
    ]
    
    const hasOpening = (appts: string[]) => appts.length < 3
    const findLastOpening = findLastKey(hasOpening)
    const lastOpeningIdx = findLastOpening(appointmentsByDay)
    const lastOpeningDay = dayOfWeek[lastOpeningIdx!]
    
    console.log(lastOpeningDay)
    // is thursday
    
    const dayOfWeek = [
      'monday',
      'tuesday',
      'wednesday',
      'thursday',
      'friday',
    ]
    
    const appointmentsByDay = [
      ['9am-10', '11am-12', '1:30pm-3'],
      ['8:30am-10', '1pm-2', '2:30pm-3:30'],
      [],
      ['9:30am-10:30', '2pm-3'],
      ['8am-10', '11am-12', '1pm-3'],
    ]
    
    const hasOpening = appts => appts.length < 3
    const findLastOpening = findLastKey(hasOpening)
    const lastOpeningIdx = findLastOpening(
      appointmentsByDay
    )
    const lastOpeningDay = dayOfWeek[lastOpeningIdx]
    
    console.log(lastOpeningDay)
    // is thursday
    const dayOfWeek = [
      'monday',
      'tuesday',
      'wednesday',
      'thursday',
      'friday',
    ]
    
    const appointmentsByDay = [
      ['9am-10', '11am-12', '1:30pm-3'],
      ['8:30am-10', '1pm-2', '2:30pm-3:30'],
      [],
      ['9:30am-10:30', '2pm-3'],
      ['8am-10', '11am-12', '1pm-3'],
    ]
    
    const hasOpening = (appts: string[]) =>
      appts.length < 3
    const findLastOpening = findLastKey(hasOpening)
    const lastOpeningIdx = findLastOpening(
      appointmentsByDay
    )
    const lastOpeningDay = dayOfWeek[lastOpeningIdx!]
    
    console.log(lastOpeningDay)
    // is thursday