如何检查Ruby数组中是否存在具有特定属性的对象

时间:2022-12-04 08:20:25

I have an array of objects *and the object looks something *like this {seat_id, room_id, date_created};
I want to find if in that array there is an object which has seat_id equal to a specific value. How can I do that?

我有一个对象数组*,对象看起来像* {seat_id,room_id,date_created};我想找到是否在该数组中有一个对象,其seat_id等于特定值。我怎样才能做到这一点?

3 个解决方案

#1


63  

arr.any?{|a| a.seat_id == "value"}

#2


19  

Here:

这里:

arr.find_index {|item| item.seat_id == other.seat_id}

#3


1  

arr.map{|a| a.seat_id == "value"}

It will return array of true and false value, true value are matching value.

它将返回true和false值的数组,true值是匹配值。

#1


63  

arr.any?{|a| a.seat_id == "value"}

#2


19  

Here:

这里:

arr.find_index {|item| item.seat_id == other.seat_id}

#3


1  

arr.map{|a| a.seat_id == "value"}

It will return array of true and false value, true value are matching value.

它将返回true和false值的数组,true值是匹配值。