findLast

findLast

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

    const lt3 = n => n < 3
    const findLastLt3 = findLast(lt3)
    
    findLastLt3([1, 2, 3]) // is 2
    findLastLt3([3, 4, 5]) // is undefined
    const lt3 = (n: number) => n < 3
    const findLastLt3 = findLast(lt3)
    
    findLastLt3([1, 2, 3]) // is 2
    findLastLt3([3, 4, 5]) // is undefined
    
    const lt3 = n => n < 3
    const findLastLt3 = findLast(lt3)
    
    findLastLt3([1, 2, 3]) // is 2
    findLastLt3([3, 4, 5]) // is undefined
    const lt3 = (n: number) => n < 3
    const findLastLt3 = findLast(lt3)
    
    findLastLt3([1, 2, 3]) // is 2
    findLastLt3([3, 4, 5]) // is undefined
    
  2. Sometimes we want to find the last value that passes a condition. Below, we have some of Jim Carrey's oldest films ordered by release date. Let's find his oldest film, which wasn't a comedy.

    const movies = getMovies()
    
    const isNotComedy = movie => !movie.categories.includes('comedy')
    const findOldestNonComedy = findLast(isNotComedy)
    const oldestNonComedy = findOldestNonComedy(movies)
    
    console.log(oldestNonComedy.title)
    // is "Mike Hammer: Murder Takes All"
    
    function getMovies() {
      return [
        {
          title: 'Doing Time on Maple Drive',
          categories: ['drama'],
          released: 1992,
        },
        {
          title: 'Mike Hammer: Murder Takes All',
          categories: ['crime', 'mystery', 'thriller'],
          released: 1989,
        },
        {
          title: 'Copper Mountain',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'All In Good Taste',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'Rubberface',
          categories: ['comedy', 'drama'],
          released: 1981,
        },
      ]
    }
    type Movie = {
      title: string
      categories: string[]
      released: number
    }
    
    const movies = getMovies()
    
    const isNotComedy = (movie: Movie) => !movie.categories.includes('comedy')
    const findOldestNonComedy = findLast(isNotComedy)
    const oldestNonComedy = findOldestNonComedy(movies)
    
    console.log(oldestNonComedy?.title)
    // is "Mike Hammer: Murder Takes All"
    
    function getMovies(): Movie[] {
      return [
        {
          title: 'Doing Time on Maple Drive',
          categories: ['drama'],
          released: 1992,
        },
        {
          title: 'Mike Hammer: Murder Takes All',
          categories: ['crime', 'mystery', 'thriller'],
          released: 1989,
        },
        {
          title: 'Copper Mountain',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'All In Good Taste',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'Rubberface',
          categories: ['comedy', 'drama'],
          released: 1981,
        },
      ]
    }
    
    const movies = getMovies()
    
    const isNotComedy = movie =>
      !movie.categories.includes('comedy')
    const findOldestNonComedy = findLast(isNotComedy)
    const oldestNonComedy =
      findOldestNonComedy(movies)
    
    console.log(oldestNonComedy.title)
    // is "Mike Hammer: Murder Takes All"
    
    function getMovies() {
      return [
        {
          title: 'Doing Time on Maple Drive',
          categories: ['drama'],
          released: 1992,
        },
        {
          title: 'Mike Hammer: Murder Takes All',
          categories: [
            'crime',
            'mystery',
            'thriller',
          ],
          released: 1989,
        },
        {
          title: 'Copper Mountain',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'All In Good Taste',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'Rubberface',
          categories: ['comedy', 'drama'],
          released: 1981,
        },
      ]
    }
    type Movie = {
      title: string
      categories: string[]
      released: number
    }
    
    const movies = getMovies()
    
    const isNotComedy = (movie: Movie) =>
      !movie.categories.includes('comedy')
    const findOldestNonComedy = findLast(isNotComedy)
    const oldestNonComedy =
      findOldestNonComedy(movies)
    
    console.log(oldestNonComedy?.title)
    // is "Mike Hammer: Murder Takes All"
    
    function getMovies(): Movie[] {
      return [
        {
          title: 'Doing Time on Maple Drive',
          categories: ['drama'],
          released: 1992,
        },
        {
          title: 'Mike Hammer: Murder Takes All',
          categories: [
            'crime',
            'mystery',
            'thriller',
          ],
          released: 1989,
        },
        {
          title: 'Copper Mountain',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'All In Good Taste',
          categories: ['comedy'],
          released: 1983,
        },
        {
          title: 'Rubberface',
          categories: ['comedy', 'drama'],
          released: 1981,
        },
      ]
    }