0

Can I get all hash in array like this:

a = [[[{id => "1", com => "abcd"}],[{id => "2", com => "rty"},{id => "3", com => "mnq"},...]], "products"]

I want to get hash only like this :

b = [{id => "1", com => "abcd"},{id => "2", com => "rty"},{id => "3", com => "mnq"},...]

Please! tell me how to do that in ruby on rails :)

1 Answer 1

1

For this you can use the #flatten or #flatten! methods found for Array's in Ruby

So just use

a.flatten

and the result will be what is shown in b

More information can be found here.

Sign up to request clarification or add additional context in comments.

1 Comment

As the last element of a is not a hash, it appears you need an additional step: a.flatten.select { |obj| obj.is_a? Hash }.

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.