8

我有一个包含匿名元素的数组。元素通过 php 添加到数组中,如下所示:

$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

然后我用 ajax post 在 jquery 中检索这个数组。一切正常。

现在,我正在寻找一种在 javascript/jquery 中将所有数组元素作为对处理/输出的方法。为每个第二个元素“做某事”。像这样:

foreach (two_elements_in_array) {
  // output track name
  // output track time
  // output some divider
}

如何才能做到这一点?

4

9 回答 9

7

好吧,也许这是最基本的解决方案:

for (var i = 0; i < arr.length; i += 2) {
    var title = arr[i];
    var len = arr[i+1];
}

但是,我建议您安排$playlist如下:

while (databaseloop) {
    $playlist[] = array(
        "title" => $a_title,
        "length" => $a_length
    );
}

然后很容易简单地迭代元素:

for (var i = 0; i < arr.length; i++) {
    var title = arr[i]['title'];
    var len = arr[i]['length'];
}
于 2012-05-28T14:53:10.853 回答
2

您可以将数组拆分为一个由二元素数组组成的数组。

var arr = ["Hello.mp3", "00:00:14", "Byebye.mp3", "00:00:30", "Whatsup.mp3", "00:00:07", "Goodnight.mp3", "00:00:19"];
arr.map(function(elem,i,arr){return [elem, (i+1<arr.length) ? arr[i+1] : null];})
    .filter(function(elem,i){return !(i%2);});
于 2013-05-03T16:52:57.890 回答
1

使用Array.prototype.reduce()

let pairs = playlist.reduce((list, _, index, source) => {
    if (index % 2 === 0) {
        list.push(source.slice(index, index + 2));
    }
    return list;
}, []);

这为您提供了一个可以使用的二维数组pairs

于 2018-01-17T12:37:45.137 回答
0

不与foreach.

for (var i = 0; i < array.length; i += 2) {
    var name = array[i];
    var time = array[i + 1];
    // do whatever
}
于 2012-05-28T14:53:11.257 回答
0

一个增量为 2 的简单 for 循环。您可能需要确保您的数组长度对于 i+1 足够长,以防长度不能被 2 整除!

for (i = 0; i+1 < array.length; i+=2) {
  name = array[i];
  length = array[i+1];
}
于 2012-05-28T14:54:33.260 回答
0
var arr = ["Hello.mp3", "00:00:14", "Byebye.mp3", "00:00:30", "Whatsup.mp3", "00:00:07", "Goodnight.mp3", "00:00:19"];

var group = [];
for (var x = 0; x < arr.length; x += 2) {
    var track = arr[x],
        length = arr[x + 1];
    group.push({
       track: track,
       length: length
    })
}
于 2012-05-28T14:56:13.863 回答
0

正如@VisioN所说,如果您使用关联数组会更容易,否则您可以在客户端迭代时拥有一个单独的标签数组

var d=["Hello.mp3", "00:00:14", "Byebye.mp3", "00:00:30", "Whatsup.mp3", "00:00:07", "Goodnight.mp3", "00:00:19"] ;
var e=["name","time"];
var c=0;
$.each(d,function(i,j){
    if(c>=2)c=0;
    console.log(e[c]+j);
    c++;
});

http://jsfiddle.net/FmLYk/1/

于 2012-05-28T15:00:36.903 回答
0

如果可能的话,我建议您对代码进行更多优化,以使其更有意义并且不易出错。这也是更加面向对象的好方法!

像这样(我在这里使用 jQuery):

var array = [ {name: "Hello.mp3", time: "00:00:14"}, {name: "Byebye.mp3", time:"00:00:30"}, {name: "Whatsup.mp3", time: "00:00:07"}, {name: "Goodnight.mp3", time: "00:00:19"}];

然后你就可以循环它并生成看起来更干净的代码

array.forEach(function(data){
    //edit the output here
    console.log(data.name + " " + data.time );  
});
于 2012-05-28T15:00:58.390 回答
0

破坏性的,但干净的:

while (arr.length) {
  const Title  = arr.shift();
  const Length = arr.shift();

  // Do work.
}
于 2017-04-15T23:25:48.533 回答