0

I've got an array in the following form:

[["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]

Is there a way to somehow display this information on a Rails template using iterators? I need something like this:

First - a, b, c

Second - d, e,

Third - g, h, i.

Or this is impossible and I should modify the initial array form?

Thanks in advance.

2
  • Have you tried it? Commented Jun 4, 2017 at 19:06
  • @Iceman That's the case, I have no idea how to execute this. Commented Jun 4, 2017 at 19:08

1 Answer 1

1

Without modifing the main array you could try with each_with_index inside each for the main array of arrays, then checking for the first value you can skip it and get the array of letters:

array = [["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]

array.each do |main|
  main.each_with_index do |value, index|
    next if index.zero?
    p value 
  end
end
# => ["a", "b", "c"]
#    ["d", "e"]
#    ["g", "h", "i"]

Or if you want to access it as a hash it'd be easier:

array = [["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]
array.to_h.each do |_, value|
  p value
end
# => ["a", "b", "c"]
#    ["d", "e"]
#    ["g", "h", "i"]
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome, I think it'd be better to iterate as 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.