Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于动态数据的java脚本循环_Javascript_Jquery - Fatal编程技术网

Javascript 用于动态数据的java脚本循环

Javascript 用于动态数据的java脚本循环,javascript,jquery,Javascript,Jquery,我想在给定的java脚本中进行循环,以动态地从文件夹(如song)中获取数据 <script type="text/javascript"> Amplitude.init({ "songs": [ { "name": "Living Proof", "artist": "Gregory Alan Isakov",

我想在给定的java脚本中进行循环,以动态地从文件夹(如song)中获取数据

<script type="text/javascript">
        Amplitude.init({
            "songs": [
                {
                    "name": "Living Proof",
                    "artist": "Gregory Alan Isakov",
                    "album": "The Weatherman",
                    "url": "HH.mp3",
                    "live": false,
                    "cover_art_url": "images/theweatherman.jpg"
                },

            ],
            "default_album_art": "images/no-cover-large.png",
            "callbacks": {
                "after_init": "album_change",
                "after_album_change": "album_change",
                "after_song_ended": "album_change"
            }
        });

        function album_change(){
            var activeSong = Amplitude.getActiveSongMetadata();

            $('.album-display').hide();
            $('.album-container').removeClass('active-album-container');

            switch( activeSong.album ){
                case 'The Weatherman':
                    $('.the-weatherman-display').show();
                    $('.the-weatherman').addClass('active-album-container');
                break;
                case 'Rooms For Adelaide':
                    $('.rooms-for-adelaide-display').show();
                    $('.rooms-for-adelaide').addClass('active-album-container');
                break;
                case 'The Suburbs':
                    $('.the-suburbs-display').show();
                    $('.the-suburbs').addClass('active-album-container');
                break;
            }
        }
        $('.album-container').click(function(){
            $('.album-display').hide();
            $('.album-container').removeClass('active-album-container');

            if( $(this).hasClass('the-weatherman') ){
                $('.the-weatherman-display').show();
                $('.the-weatherman').addClass('active-album-container');
            }

            if( $(this).hasClass('rooms-for-adelaide') ){
                $('.rooms-for-adelaide-display').show();
                $('.rooms-for-adelaide').addClass('active-album-container');
            }

            if( $(this).hasClass('the-suburbs') ){
                $('.the-suburbs-display').show();
                $('.the-suburbs').addClass('active-album-container');
            }
        });
    </script>

振幅.init({
“歌曲”:[
{
“姓名”:“活生生的证明”,
“艺术家”:“格雷戈里·艾伦·伊萨科夫”,
“专辑”:“天气预报员”,
“url”:“HH.mp3”,
“活”:假,
“封面艺术网址”:“images/theweatherman.jpg”
},
],
“默认相册艺术”:“图像/无封面大.png”,
“回调”:{
“开始后”:“相册更改”,
“更改相册后”:“更改相册”,
“歌曲结束后”:“专辑更改”
}
});
函数集_change(){
var activeSong=Amplication.getActiveSongMetadata();
$('.album display').hide();
$('.album-container').removeClass('active-album-container');
开关(activeSong.album){
“天气预报员”案例:
$('.theweatherman display').show();
$('.TheWeatherman').addClass('active-album-container');
打破
案例“阿德莱德的房间”:
$('.rooms for adelaide display').show();
$('.rooms for adelaide').addClass('active-album-container');
打破
“郊区”案例:
$('.thepurpose display').show();
$('.TheSuburbies').addClass('active-album-container');
打破
}
}
$('.album container')。单击(函数(){
$('.album display').hide();
$('.album-container').removeClass('active-album-container');
if($(this).hasClass('the-weatherman')){
$('.theweatherman display').show();
$('.TheWeatherman').addClass('active-album-container');
}
if($(this).hasClass('rooms-for-adelaide')){
$('.rooms for adelaide display').show();
$('.rooms for adelaide').addClass('active-album-container');
}
if($(this).hasClass('the-substrate')){
$('.thepurpose display').show();
$('.TheSuburbies').addClass('active-album-container');
}
});

如果您指的是如何对json数据执行循环,那么可以使用jQuery中的$.each()方法,或者使用原生JS的forEach方法

见: jQuery:


原生JS:

只要获取的数据是一个对象数组,就可以使用
Array.prototype.map
Array.prototype.filter
来提取所需的数据。您的问题是什么?首先编辑它,因为格式到处都是。那么请提出一个精确的问题。如果你问如何在javascript中执行循环,那么你可能想先试试google。就个人而言,如果根对象是数组,我会使用
map
filter
reduce
。与for each循环相比,这使得深层嵌套的数据提取更加容易。我不确定这会有什么帮助。我的样本数据不一定反映OP的数据。