Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby array of arrays find by inner array value

Tags:

arrays

ruby

I have array of arrays, which looks like this:

a = [['1','1500','SomeName','SomeSurname'],
['2','1500','SomeName2','SomeSurname2'],
['3','1500','SomeName3','SomeSurname3'],
['4','1501','SomeName','SomeSurname'],
...]

I can get sub-array of this array with all rows containing '1500' value by .each function and simple if, but if a.length is large, it's taking too much time! How can I get all rows from a with certain a[1] value, without iterating over a?

like image 507
Krzysztof Witczak Avatar asked Dec 05 '22 03:12

Krzysztof Witczak


1 Answers

Enumerable#find_all is what you are looking for:

a.find_all { |el| el[1] == '1500' } # a.select will do the same
like image 147
Andrey Deineko Avatar answered Dec 06 '22 16:12

Andrey Deineko