The following code works, but can you tell me if this is the right way to do it?
I have an array of Position objects and I want to check if it contains an object which attribute 'hidden' has "false' value:
<% if positions.collect{|position| position.hidden}.include?(false) %>
...
<% end %>
The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.
To check if an element exists in an array in React: Use the includes() method to check if a primitive exists in an array. Use the some() method to check if an object exists in an array.
To search a particular object, we will use the Array prototype find method. This returns a value on a given criterion, otherwise, it returns 'undefined'. It takes two parameters, one required callback function and an optional object, which will be set as a value of this inside the callback function.
<% if positions.any?{|position| !position.hidden} %>
...
<% end %>
Using the any? method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With