first
first
(sequence: Sequence) => Value
Return the first item in a sequence
first(['a', 'b', 'c']) // is 'a' first('abc') // is 'a' first('') // is undefined
first(['a', 'b', 'c']) // is 'a' first('abc') // is 'a' first('') // is undefined
first(['a', 'b', 'c']) // is 'a' first('abc') // is 'a' first('') // is undefined
first(['a', 'b', 'c']) // is 'a' first('abc') // is 'a' first('') // is undefined
Sometimes we have sorted data and only care about the first item. Below, we ran out of bird feed, so we searched Walmart for 5 lb bags and sorted by price. Let's get the price of the cheapest one.
const results = [ { name: 'Texas Wild', price: '4.68' }, { name: 'Eastern Blend', price: '5.74' }, { name: 'Sunflower and Natural Grains', price: '6.74' }, ] const { price } = first(results) console.log(price) // 4.68
type Item = { name: string price: string } const results: Item[] = [ { name: 'Texas Wild', price: '4.68' }, { name: 'Eastern Blend', price: '5.74' }, { name: 'Sunflower and Natural Grains', price: '6.74' }, ] const { price } = first(results) as Item console.log(price) // 4.68
const results = [ { name: 'Texas Wild', price: '4.68' }, { name: 'Eastern Blend', price: '5.74' }, { name: 'Sunflower and Natural Grains', price: '6.74', }, ] const { price } = first(results) console.log(price) // 4.68
type Item = { name: string price: string } const results: Item[] = [ { name: 'Texas Wild', price: '4.68' }, { name: 'Eastern Blend', price: '5.74' }, { name: 'Sunflower and Natural Grains', price: '6.74', }, ] const { price } = first(results) as Item console.log(price) // 4.68