last

last

  • (sequence: Sequence) => Value
  1. Return the last item in a sequence

    last(['a', 'b', 'c']) // is 'c'
    last('abc') // is 'c'
    last('') // is undefined
    last(['a', 'b', 'c']) // is 'c'
    last('abc') // is 'c'
    last('') // is undefined
    
    last(['a', 'b', 'c']) // is 'c'
    last('abc') // is 'c'
    last('') // is undefined
    last(['a', 'b', 'c']) // is 'c'
    last('abc') // is 'c'
    last('') // is undefined
    
  2. Sometimes we have sorted data and only care about the last item. Below, we noticed the Harry Potter book wasn't at our library. Let's find the last person to check it out so we can follow up with them.

    const harryPotterCheckouts = [
      {
        name: 'jen',
        checkedOut: '2025-01-05',
        returned: '2025-02-02',
      },
      {
        name: 'mike',
        checkedOut: '2025-02-06',
        returned: '2025-02-25',
      },
      {
        name: 'luke',
        checkedOut: '2025-03-01',
      },
    ]
    
    const { name } = last(harryPotterCheckouts)
    console.log(name)
    // is luke
    type Checkout = {
      name: string
      checkedOut: string
      returned?: string
    }
    
    const harryPotterCheckouts: Checkout[] = [
      {
        name: 'jen',
        checkedOut: '2025-01-05',
        returned: '2025-02-02',
      },
      {
        name: 'mike',
        checkedOut: '2025-02-06',
        returned: '2025-02-25',
      },
      {
        name: 'luke',
        checkedOut: '2025-03-01',
      },
    ]
    
    const { name } = last(harryPotterCheckouts) as Checkout
    console.log(name)
    // is luke
    
    const harryPotterCheckouts = [
      {
        name: 'jen',
        checkedOut: '2025-01-05',
        returned: '2025-02-02',
      },
      {
        name: 'mike',
        checkedOut: '2025-02-06',
        returned: '2025-02-25',
      },
      {
        name: 'luke',
        checkedOut: '2025-03-01',
      },
    ]
    
    const { name } = last(harryPotterCheckouts)
    console.log(name)
    // is luke
    type Checkout = {
      name: string
      checkedOut: string
      returned?: string
    }
    
    const harryPotterCheckouts: Checkout[] = [
      {
        name: 'jen',
        checkedOut: '2025-01-05',
        returned: '2025-02-02',
      },
      {
        name: 'mike',
        checkedOut: '2025-02-06',
        returned: '2025-02-25',
      },
      {
        name: 'luke',
        checkedOut: '2025-03-01',
      },
    ]
    
    const { name } = last(
      harryPotterCheckouts
    ) as Checkout
    console.log(name)
    // is luke