Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.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中提取元素_Javascript_Arrays_Loops - Fatal编程技术网

从数组javascript中提取元素

从数组javascript中提取元素,javascript,arrays,loops,Javascript,Arrays,Loops,我对javascript完全陌生,所以请容忍我 我有一个传递了变量的函数,如下所示: function.getDestination( { destinations: [theFirstDestination, theSecondDestination], } 通过显式定义变量,我可以传入任意数量的变量。我还有一个数组,它保存我要传入的值,destinationArray。同样,我可以通过从数组显式调用这些变量来传递这些变量: function.getDestina

我对javascript完全陌生,所以请容忍我

我有一个传递了变量的函数,如下所示:

function.getDestination(
    {
      destinations: [theFirstDestination, theSecondDestination],
    }
通过显式定义变量,我可以传入任意数量的变量。我还有一个数组,它保存我要传入的值,
destinationArray
。同样,我可以通过从数组显式调用这些变量来传递这些变量:

function.getDestination(
    {
      destinations: [destinationArray[0], destinationArray[1],
    }

我想做的是循环遍历整个数组,并将每个变量传递到函数中:有没有比手动键入每个索引更简单的方法?

如果愿意,可以执行
目标:destinationArray
,但是一侧的更改会影响另一侧。如果您想要完全独立的副本:

destinations: destinationArray.slice(0)

我认为您要做的是将数组复制到
目的地
。在这种情况下,只需使用:

{
     destinations: destinationArray.slice(0)
}
代码未经测试(现在无法),但应该可以工作

function getDestination(param) {
    for(var i=0;i<param.destinations.length;i++) {

        // do something with i (the number)
        // or with param.destinations[i] (the value)

        console.log(i+": "+param.destinations[i]);
    }
}
你这样称呼其中一个:

getDestination({destinations: [theFirstDestination, theSecondDestination]});

谢谢,它没有正确通过它们。我还应该注意,如果变量被放在方括号内,这会有什么影响吗?
getDestination({destinations: [theFirstDestination, theSecondDestination]});