Javascript将数组作为参数传递给具有多个参数的函数

时间:2015-10-23 17:57:14

标签: javascript

我正在尝试将字符串变量和两个数组传递给全局函数。像这样:

...
var stop_name = "temp";
var times = new Array(json.length);
var headsigns = new Array(json.length);
...
if(times.length < 6){
  send_times_to_device(stop_name, times, headsigns);
}
...

稍后在代码中:

{{1}}

如何在Javascript中正确执行此操作?

提前致谢!

编辑: 你们是对的,我的代码中的其他地方有一个错误,这有效!

1 个答案:

答案 0 :(得分:1)

new Array(n),创建一个包含n个未定义条目的数组。

如果要创建一个具有整数值的数组作为第一个条目,请使用:

function send_times_to_device(stop_name, times, headsigns) {
  // function code here
    console.log(stop_name);
    console.log(times);
    console.log(headsigns);
}

var stop_name = "temp";
var times = [json.length];
var headsigns = [json.length];

send_times_to_device(stop_name, times, headsigns);
相关问题