Lets say I have the following:
nested_object = [0, 1, 2, {foo: 'bar'}]
How do I use dig to select the last element of the array as I search for deeper nested objects?
A negative value integer in any dig method arguments starts at the end of the array and moves towards the beginning. So -1
selects the last element of the array, -2
selects the second from last, and so on. So, in your case:
target = nested_object.dig(-1, :foo)
will select the last element of the array and proceed from there.
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