Looping through array and output as pairs (divider for each second element)

后端 未结 9 543
旧时难觅i
旧时难觅i 2021-01-18 03:41

I have an array with anonymous elements. Elements are added to the array via php, like so:

$playlist = array();

while (databaseloop) {
  $playlist[] = $a_ti         


        
9条回答
  •  滥情空心
    2021-01-18 04:12

    I would suggest that you optimize your code a little bit more so that it would make a bit more sense and be less error prone, if that's possible that is. This is also a great way to be more object-oriented!

    Like this (I'm using jQuery here) :

    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"}];
    

    Then you would be able to loop over it and produce a bit more clean looking code

    array.forEach(function(data){
        //edit the output here
        console.log(data.name + " " + data.time );  
    });
    

提交回复
热议问题