28

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?

3 Answers 3

72
arr.any?{|a| a.seat_id == "value"}
Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer because you want to find IF there is the element. But if you want to find WHERE the element is, the other answer is the correct one.
I want to find IF there is an element. Thanks @davidrac
20

Here:

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

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.