Javascript 如何将.each()用于带列表的循环?

Javascript 如何将.each()用于带列表的循环?,javascript,jquery,html,arrays,django,Javascript,Jquery,Html,Arrays,Django,我有一个列表,我正在尝试迭代,以将每个div更改为自己的歌曲。我的列表用JSON简化,因为数据来自Django查询集。我的问题是我不知道如何继续下去 不让下一个对象在循环中重复同一个对象 我试过设置变量来阻止它,或者转到下一首歌,但没有成功 The Json as data variable: [{"song":{"file":"music/the_unlighteds_2.mp3"}},{"song":{"file":"music/the_unlighted_6_32.mp3"}}] 脚本

我有一个列表,我正在尝试迭代,以将每个div更改为自己的歌曲。我的列表用JSON简化,因为数据来自Django查询集。我的问题是我不知道如何继续下去 不让下一个对象在循环中重复同一个对象

我试过设置变量来阻止它,或者转到下一首歌,但没有成功

The Json as data variable:  [{"song":{"file":"music/the_unlighteds_2.mp3"}},{"song":{"file":"music/the_unlighted_6_32.mp3"}}]
脚本代码:
$(“h1[id]”)。每个(函数(){
var next_song=“”;
var data=[{{chap_song | safe}}]
控制台日志(数据);
用于(数据中的var i){
var next_song=JSON.stringify(数据[i].song.file);
}
console.log(下一首歌);

如果(this.id==='scroll-to')$(this.html(“),您可以尝试使用相同的循环索引来访问两个数组中的数据

var data = [{{ chap_song|safe }}];
$("h1[id]").each(function(i){ // i is the index of the current h1
  var song = data[i]; // so we want the corresponding song
  $(this).html("<audio controls autoplay> <source src=https://theunlighted.io/media/"+song.file.replace(/\"/g, "")+ " type=\"audio/wav\"></audio>");
});
var data=[{{{chap_song | safe}}];
$(“h1[id]”)。每个(函数(i){//i是当前h1的索引
var song=data[i];//因此我们需要相应的歌曲

$(this.html(“您可以尝试使用相同的循环索引来访问两个数组中的数据

var data = [{{ chap_song|safe }}];
$("h1[id]").each(function(i){ // i is the index of the current h1
  var song = data[i]; // so we want the corresponding song
  $(this).html("<audio controls autoplay> <source src=https://theunlighted.io/media/"+song.file.replace(/\"/g, "")+ " type=\"audio/wav\"></audio>");
});
var data=[{{{chap_song | safe}}];
$(“h1[id]”)。每个(函数(i){//i是当前h1的索引
var song=data[i];//因此我们需要相应的歌曲
$(this.html(“
$”(“h1[id]”)。每个(函数(索引){
var next_song=[];
var data=[{“song”:{“file”:“music/the_unlighted_2.mp3”},{“song”:{“file”:“music/the_unlighted_6_32.mp3”}]
控制台日志(数据);
用于(数据中的var i){
next_song.push(JSON.stringify(data[i].song.file));
}
log(next_song[index]);
if(this.id=='scroll-to')$(this.html(“
$”(“h1[id]”)。每个(函数(索引){
var next_song=[];
var data=[{“song”:{“file”:“music/the_unlighted_2.mp3”},{“song”:{“file”:“music/the_unlighted_6_32.mp3”}]
控制台日志(数据);
用于(数据中的var i){
next_song.push(JSON.stringify(data[i].song.file));
}
log(next_song[index]);
if(this.id=='scroll-to')$(this.html(“
$("h1[id]").each(function(index){
            var next_song = [];
            var data = [{"song":{"file":"music/the_unlighteds_2.mp3"}},{"song":{"file":"music/the_unlighted_6_32.mp3"}}]
            console.log(data);
            for (var i in data) {

            next_song.push(JSON.stringify(data[i].song.file));


            }
            console.log(next_song[index]);
            if(this.id==='scroll-to')$(this).html("<audio controls autoplay> <source src=https://theunlighted.io/media/"+next_song[index].replace(/\"/g, "")+ " type=\"audio/wav\"></audio>");

});