本文介绍了如何迭代数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迭代数组的最佳方法是什么?

What's the best way to iterate over an array of arrays?

sounds = [ [Name_1, link_1], [Name_2, link_2], [Name_3, link_3], [Name_4, link_4] ]

我想以 HTML ul/li 结构输出:

I want to the output in HTML ul/li structure:

<ul>
   <li>Name_1, link_1</li>
   <li>Name_2, link_2</li>
   <li>Name_3, link_3</li>
   <li>Name_4, link_4</li>
</ul>

推荐答案

假设所有内部数组的大小都是固定的,那么在对外部数组进行迭代时,可以使用自动解包将内部数组的每一项都放到自己的变量中.示例:

Assuming all the inner arrays have fixed size, you can use auto-unpacking to get each item of the inner array in its own variable when iterating over the outer array. Example:

sounds.each do |name, link|
  # do something
end

这篇关于如何迭代数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:08
查看更多