Get the first element in an array?

Answered
timmy123 asked this question 4 months ago
timmy123 on Jan 19

Given an array like this:

["one", "two", "three", "four", "five"]

How do I get the first string/item in that array?

I'm coding in JavaScript.

4 suggested answers
yaboy01 on Jan 20

Access the array at the 0 index, which is the first element in the array:

let array = ["one", "two", "three", "four", "five"]

array[0] // "one"
0 replies
nick on Jan 20
["one", "two", "three", "four", "five"][0]
0 replies
looper003 on Jan 20

Lodash has a head() method you can use:

_.head(array) // returns first element in an array
0 replies
moon_man41 on Jan 20

An additional way that works using find():

array.find(e => true)
0 replies
Answered