Home  >  Q&A  >  body text

ruby数组构造问题

大类别中的数据循环,小类别再循环。

想构造的形势是:

c_array = [[100,200,300],[400,500],[600,700,800,900]]

这里[100,200,300]是category0的,[400,500]是category1的,[600,700,800,900]是category2的。

代码:

c_array = Array.new
co_array = Array.new
@categories.each do |c|
  c.others.each do |co|
    co_array << co.price
  end
  c_array << co_array
end
puts c_array.inspect

循环得到的结果是:

c_array = [[100,200,300,400,500,600,700,800,900],[100,200,300,400,500,600,700,800,900],[100,200,300,400,500,600,700,800,900]]

怎么循环能达到预期效果呢?

伊谢尔伦伊谢尔伦2762 days ago503

reply all(0)I'll reply

No reply
  • Cancelreply