Finding Elements Nested Inside Multiple Arrays in Ruby

0
2566

This coding exercise tests your knowledge of iterating through nested arrays collections and implementing a basic search algorithm.

Summary

Add a method to the Array class that searches for a nested array element.

Exercise File

Code File

Exercise Description

Build a method that returns an array from a nested array if the second element equals the queried element.

Example

players = [
  [27, 'Jose Altuve'],
  [2,  'Alex Bregman'],
  [1,    'Carlos Correa'],
  [9,    'Marwin Gonzalez'],
  [10, 'Yulieski Gurriel']
]

find_element(players, 'Jose Altuve')
# [27, 'Jose Altuve']

Real World Usage

This exercise has a two-fold purpose:

  1. To work with nested data collections.
  2. So that you can learn about a rarely used Ruby method.

Solution

Can be found on the solutions branch on github.

LEAVE A REPLY

Please enter your comment!
Please enter your name here