[英]How to loop an array inside another array in Javascript?
Given this script鉴于此脚本
<script>
var sentences=
[
['journey27.mp4',
[
[1, 2],
[5, 7]
]
],
['journey28.mp4',
[
[10, 12],
[13, 17],
[25, 36]
]
],
['journey30.mp4',
[
[13, 15],
[15, 19]
]
]
];
function playVideo(myUrl, startTime, endTime) {
console.log(`Playing ${myUrl} from ${startTime} to ${endTime}`);
}
function myLoop(){
//what should I fill in here?
}
</script>
I want to print out this result:我想打印出这个结果:
Playing 'journey27.mp4' from 1 to 2从 1 到 2 播放“journey27.mp4”
Playing 'journey27.mp4' from 1 to 2从 1 到 2 播放“journey27.mp4”
Playing 'journey27.mp4' from 1 to 2从 1 到 2 播放“journey27.mp4”
Playing 'journey27.mp4' from 5 to 7从 5 到 7 播放“journey27.mp4”
Playing 'journey27.mp4' from 5 to 7从 5 到 7 播放“journey27.mp4”
Playing 'journey27.mp4' from 5 to 7从 5 到 7 播放“journey27.mp4”
Playing 'journey28.mp4' from 10 to 12从 10 点到 12 点播放“journey28.mp4”
Playing 'journey28.mp4' from 10 to 12从 10 点到 12 点播放“journey28.mp4”
Playing 'journey28.mp4' from 10 to 12从 10 点到 12 点播放“journey28.mp4”
Playing 'journey28.mp4' from 13 to 17从 13 到 17 播放“journey28.mp4”
Playing 'journey28.mp4' from 13 to 17从 13 到 17 播放“journey28.mp4”
Playing 'journey28.mp4' from 13 to 17从 13 到 17 播放“journey28.mp4”
Playing 'journey28.mp4' from 25 to 36从 25 到 36 播放“journey28.mp4”
Playing 'journey28.mp4' from 25 to 36从 25 到 36 播放“journey28.mp4”
Playing 'journey28.mp4' from 25 to 36从 25 到 36 播放“journey28.mp4”
Playing 'journey30.mp4' from 13 to 15从 13 到 15 播放“journey30.mp4”
Playing 'journey30.mp4' from 13 to 15从 13 到 15 播放“journey30.mp4”
Playing 'journey30.mp4' from 13 to 15从 13 到 15 播放“journey30.mp4”
Playing 'journey30.mp4' from 15 to 19从 15 到 19 播放“journey30.mp4”
Playing 'journey30.mp4' from 15 to 19从 15 到 19 播放“journey30.mp4”
Playing 'journey30.mp4' from 15 to 19从 15 到 19 播放“journey30.mp4”
Note : each line of the subarray should be played 3 times then it moves to the next line.注意:子数组的每一行应该播放 3 次然后移动到下一行。
How can I fill in myLoop
function to print out the above result?如何填写
myLoop
function 以打印出上述结果?
You can try this approach你可以试试这个方法
var data = [ ['journey27.mp4', [ [1, 2], [5, 7] ] ], ['journey28.mp4', [ [10, 12], [13, 17], [25, 36] ] ], ['journey30.mp4', [ [13, 15], [15, 19] ] ] ]; function playVideo(myUrl, startTime, endTime) { console.log(`Playing ${myUrl} from ${startTime} to ${endTime}`); } function myLoop(sentences, lineCount) { for (const sentence of sentences) { const [video, timers] = sentence //extract video url and the timer array for (const timer of timers) { const [startTime, endTime] = timer //extract start time and end time in each timer for (let i = 0; i < lineCount; i++) { playVideo(video, startTime, endTime) //console.log for each line } } } } const lineCount = 3 //will print 3 times myLoop(data, lineCount)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.