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

后端 未结 3 595
后悔当初
后悔当初 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:53

    For first case,

    array_of_objects.reject(&:completed)
    

    For second case,

    array_of_objects.select(&:completed)
    

提交回复
热议问题