Prabhakaran
Prabhakaran

Reputation: 4013

How to merge an array of arrays together into a single array?

I have the following array

[["convertible", "2010", "red"], ["convertible", "2010", "green"]]

How do I merge the above array into this, in either in Rails or in Ruby?

["convertible", "2010", "red", "convertible", "2010", "green"]

Edit-1

@category.each do |content|
      form_chain = JSON.parse(content.content)
      chained_array << form_chain.values
    end

    chained_array

This gives the output

[["convertible", "2010", "red"], ["convertible", "2010", "green"]]

If I use chained_array.flatten! it gives the same result.

Upvotes: 1

Views: 461

Answers (2)

hirolau
hirolau

Reputation: 13901

Based on your edit you could just create a flat array from the beginning:

@category.each do |content|
      form_chain = JSON.parse(content.content)
      chained_array.push(*form_chain.values)
end

Upvotes: 0

Alex.Bullard
Alex.Bullard

Reputation: 5563

[["convertible", "2010", "red"], ["convertible", "2010", "green"]].flatten!

Upvotes: 3

Related Questions