I have a struct like this:
Struct.new("Test", :loc, :type, :hostname, :ip)
clients = [
Struct::TestClient.new(1, :pc, "pc1", "192.168.0.1")
Struct::TestClient.new(1, :pc, "pc2", "192.168.0.2")
Struct::TestClient.new(1, :tablet, "tablet1", "192.168.0.3")
Struct::TestClient.new(1, :tablet, "tablet2", "192.168.0.3")
and etc...
]
If I want to get the IP address of all devices, I can use test_clients.map(&:ip)
. How do I select the IP addresses of specific devices, say all device types called "tablet"
? How can I do that with map
?
To use a condition inside map() in React:Call the map() method on an array. Use a ternary operator to check if the condition is truthy. The operator returns the value to the left of the colon if the condition is truthy, otherwise the value to the right is returned.
In React, the map method is used to traverse and display a list of similar objects of a component. A map is not a feature of React. Instead, it is the standard JavaScript function that could be called on an array. The map() method creates a new array by calling a provided function on every element in the calling array.
We used a ternary operator to conditionally render multiple elements. The ternary operator is very similar to an if/else statement. If the value to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise the value to the right of the colon is returned. Copied!
Do a select
first
clients.select{|c| c.type == 'tablet'}.map(&:ip)
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