I have an array of objects, and I want to find which element in the array has a specific attribute equal to the value, in particular, which element in this array has an object with :parent_id equal to 55 .
:parent_id
55
How can i do this?
To find the index:
array.index{ |item| item.parent_id == 55 }
To find an item:
array.find{ |item| item.parent_id == 55 }
array.collect{|a| a[:parent_id]==55 ? a : nil}.compact!
must complete the task. First, collect all the elements that match your criteria in a new array, than delete the elements of false positives (nil).
I would use Enumerable # select Documents
results = my_array.select do |item| item[:parent_id] == 55 end
Source: https://habr.com/ru/post/906433/More articles:delayed_job does not work (rails 3.1.3) - ruby-on-railsWhy is GMT the standard for computer time instead of UTC? - timezoneTypical classes and records as interfaces - haskellThe recommended way to initialize a variable in if block is pythonthe subset () of a vector in R - rReload data from a store in ExtJS 4 - refreshIs there a way to find that the table on the page is a dataTable? - javascriptWhat is the best way to create drawings for different dpi? - androidCan Eclipse automatically generate different DPI images for Android projects? - androidHow to check if the screen touched? - androidAll Articles