通过数组和输出循环作为对(分每一秒元素) [英] Looping through array and output as pairs (divider for each second element)
本文介绍了通过数组和输出循环作为对(分每一秒元素)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有匿名元素的数组。元素通过PHP添加到阵列,像这样:
I have an array with anonymous elements. Elements are added to the array via php, like so:
$playlist = array();
while (databaseloop) {
$playlist[] = $a_title;
$playlist[] = $a_length;
}
echo json_encode(array('playlist'=>$playlist));
所以数组变为:
["Hello.mp3", "00:00:14", "Byebye.mp3", "00:00:30", "Whatsup.mp3", "00:00:07", "Goodnight.mp3", "00:00:19"] and so on
然后,我检索该数组jQuery中使用Ajax后。所有这一切工作正常。
Then I retrieve this array in jquery with ajax post. All that works fine.
现在,我正在寻找一种方式来对待/输出所有数组元素在JavaScript / jQuery的对。 做什么每个第二个元素。像这样的:
Now, I'm looking for a way to treat/output all array elements as pairs in javascript/jquery. "do something" for each second element. Like this:
foreach (two_elements_in_array) {
// output track name
// output track time
// output some divider
}
如何才能做到这一点?
How can this be done?
推荐答案
好吧,也许这是最基础的解决方案:
Well, maybe this is the most basic solution:
for (var i = 0; i < arr.length; i += 2) {
var title = arr[i];
var len = arr[i+1];
}
不过,我会建议你安排 $播放列表
如下:
while (databaseloop) {
$playlist[] = array(
"title" => $a_title,
"length" => $a_length
);
}
然后,它会很容易与简单迭代的元素:
Then it will be easy to iterate the elements simply with:
for (var i = 0; i < arr.length; i++) {
var title = arr[i]['title'];
var len = arr[i]['length'];
}
这篇关于通过数组和输出循环作为对(分每一秒元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文