Radash
  1. Array
  2. collectFirst

Basic usage

Given an array and a transform function, immediately returns the first transformed element that does not evaluate to undefined.

import { collectFirst } from 'radash'

const gods = [
  {
    name: 'Ra',
    culture: 'egypt'
  },
  {
    name: 'Zeus',
    culture: 'greek'
  },
  {
    name: 'Loki',
    culture: 'greek'
  }
]

collectFirst(gods, g =>
  g.culture === 'greek' ? g.name.toUpperCase() : undefined
) // => 'ZEUS'