如何将数组数组合并到一个数组中?

时间:2023-01-16 12:43:02

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?

如何将上面的数组合并到Rails或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.

如果我使用chained_array.flatten!它给出了相同的结果。

2 个解决方案

#1


3  

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

#2


0  

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

#1


3  

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

#2


0  

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