The assoc
method for Ruby arrays takes a single argument and searches for the matching element that is an array whose first element matches the argument. It then returns either the matching sub-array or nil
if none was found.
This is a fairly common task in situations where a hash is used to collect a set of data and the keys are used as unique identifiers. I chose this method because I am tired of writing some form of the same basic function in JavaScript:
let beardLengths = [
{name: 'cory', length: 5},
{name: 'justin', length: 4}