1
我有這樣的HTML:如何通過嵌套項目迭代
<div class="date">
<h3 class="date-title">Today</h3>
<div class="film">
<img class="poster" src="film1" />
<h4 class="title">Film 1</h4>
<ul class="session-times">
<li>
<a href="#">
<time>12:00 PM</time>
</a>
</li>
</ul>
</div><!-- /.film -->
<div class="film">
<img class="poster" src="film2" />
<h4 class="title">Film 2</h4>
<ul class="session-times">
<li>
<a href="#">
<time>3:00 PM</time>
</a>
</li>
</ul>
</div><!-- /.film -->
<div class="film">
<img class="poster" src="film3" />
<h4 class="title">Film 3</h4>
<ul class="session-times">
<li>
<a href="#">
<time>6:00 PM</time>
</a>
</li>
</ul>
</div><!-- /.film -->
</div><!-- /.date -->
<div class="date">
<h3 class="date-title">Tomorrow</h3>
<div class="film">
<img class="poster" src="film1" />
<h4 class="title">Film 1</h4>
<ul class="session-times">
<li>
<a href="#">
<time>2:00 PM</time>
</a>
</li>
</ul>
</div><!-- /.film -->
<div class="film">
<img class="poster" src="film2" />
<h4 class="title">Film 2</h4>
<ul class="session-times">
<li>
<a href="#">
<time>5:00 PM</time>
</a>
</li>
</ul>
</div><!-- /.film -->
<div class="film">
<img class="poster" src="film3" />
<h4 class="title">Film 3</h4>
<ul class="session-times">
<li>
<a href="#">
<time>8:00 PM</time>
</a>
</li>
</ul>
</div><!-- /.film -->
</div><!-- /.date -->
,我使用這個Ruby代碼中提取數據:
這給了我:
[
{
"date": "Today"
},
{
"date": "Tomorrow"
}
]
但我想循環播放各個.film
部分中的n
三部電影,並將它們包含在o輸出,所以它應該看起來更像這樣:
[
{
"Today": {
"films": [
{
"film": "Film1",
"time": "12:00 PM"
},
{
"film": "Film2",
"time": "15:00 PM"
},
{
"film": "Film3",
"time": "6:00 PM"
}
]
},
{
"Tomorrow": {
"films": [
{
"film": "Film1",
"time": "14:00 PM"
},
{
"film": "Film2",
"time": "5:00 PM"
},
{
"film": "Film3",
"time": "8:00 PM"
}
]
},
我找不出在哪裏建立我的數組嵌套循環。
太好了,謝謝! – matski
而不是折騰代碼,也提供了代碼的作用的解釋。我們的想法是教育,而不僅僅是提供解決方案。這是給某人一條魚或教他們釣魚的區別。 –
我不認爲作者無法理解我的代碼。我認爲對於初學者/非專家來說,通常很難想出如何解決問題,但向他們展示解決方案並不意味着他們無法理解解決方案。 – Ursus