根据数组长度将变量传递到函数中的问题

Problem In Passing variables into a function based on array length

提问人:Rohith M 提问时间:12/26/2022 最后编辑:Rohith M 更新时间:12/29/2022 访问量:67

问:

在下面的代码中,load_videos函数中有一个名为 createVideo 的函数,我想传递所有视频的链接,并从数组中传递称为 videos 的数组,是否可以根据数组的长度传递它。 法典:

function add_vid() {
  url_input = document.getElementById("vid_url");
  url = url_input.value;
  videos.push(url);
  url_input.value = "";
  load_videos();
}

function load_videos() {
    p_video = createVideo()
    start=true;
}

我想要的示例: 如果数组视频存储,我希望 createVideo 函数是 而 id,数组的长度增加,则 createVideo 函数中传递的视频量应该增加。['vid1, 'vid2']createVideo(videos[0], videos[1])

JavaScript 数组参数 传递 p5.js

评论

0赞 Andy 12/26/2022
为什么不直接传入函数呢?videos

答:

1赞 PeterKA 12/29/2022 #1

您可以使用运算符将元素 1 作为参数 1 发送,将元素 2 作为参数 2 发送,如下面的演示所示:...

const 
   videos = ['vid1', 'vid2'],
   createVideo = (v1,v2) => console.log( v1,v2 );
   
createVideo( ...videos );

评论

0赞 Lawrence Cherone 12/29/2022
OP想要任意长度的参数,可能更适合createVideo = function() {console.log(Object.values(arguments));}
0赞 PeterKA 12/29/2022
好点@LawrenceCherone。对于需要传递的任何数量的视频,传播运算符应该可以正常工作。我认为函数已被编写为接受可变数量的参数。这应该可以做到。createVideo