Ruby find and return objects in an array based on an attribute

后端 未结 3 599
后悔当初
后悔当初 2021-02-05 01:20

How can you iterate through an array of objects and return the entire object if a certain attribute is correct?

I have the following in my rails app

arr         


        
3条回答
  •  灰色年华
    2021-02-05 01:31

    array_of_objects.select { |favor| favor.completed == false }
    

    Will return all the objects that's completed is false.

    You can also use find_all instead of select.

提交回复
热议问题