在JavaScript中将数组作为参数传递

时间:2020-11-30 11:48:55

I have an array, and I want to pass it as a parameter in a function such as:

我有一个数组,我想将它作为参数传递给函数,例如:

function something(arrayP){
    for(var i = 0; i < arrayP.length; i++){
          alert(arrayP[i].value);
    }
 }

I'm getting that arrayP[0] is undefined, which might be true as inside the function I never wrote what kind of array arrayP is. So,

我得到的arrayP [0]是未定义的,这可能是真的,因为在函数内部我从来没有写过什么样的数组arrayP。所以,

  1. Is is possible to pass arrays as parameters?
  2. 可以将数组作为参数传递吗?
  3. If so, which are the requirements inside the function?
  4. 如果是这样,函数内部的要求是什么?

3 个解决方案

#1


34  

Just remove the .value, like this:

只需删除.value,如下所示:

function(arrayP){    
   for(var i = 0; i < arrayP.length; i++){
      alert(arrayP[i]);    //no .value here
   }
}

Sure you can pass an array, but to get the element at that position, use only arrayName[index], the .value would be getting the value property off an object at that position in the array - which for things like strings, numbers, etc doesn't exist. For example, "myString".value would also be undefined.

当然你可以传递一个数组,但是要在该位置获取元素,只使用arrayName [index],。value将从数组中该位置的对象获取value属性 - 对于字符串,数字等,等不存在。例如,“myString”.value也将是未定义的。

#2


5  

JavaScript is a dynamically typed language. This means that you never need to declare the type of a function argument (or any other variable). So, your code will work as long as arrayP is an array and contains elements with a value property.

JavaScript是一种动态类型语言。这意味着您永远不需要声明函数参数(或任何其他变量)的类型。因此,只要arrayP是一个数组并且包含具有value属性的元素,您的代码就会起作用。

#3


2  

It is possible to pass arrays to functions, and there are no special requirements for dealing with them. Are you sure that the array you are passing to to your function actually has an element at [0]?

可以将数组传递给函数,处理它们没有特殊要求。你确定要传递给函数的数组实际上有一个元素在[0]吗?

#1


34  

Just remove the .value, like this:

只需删除.value,如下所示:

function(arrayP){    
   for(var i = 0; i < arrayP.length; i++){
      alert(arrayP[i]);    //no .value here
   }
}

Sure you can pass an array, but to get the element at that position, use only arrayName[index], the .value would be getting the value property off an object at that position in the array - which for things like strings, numbers, etc doesn't exist. For example, "myString".value would also be undefined.

当然你可以传递一个数组,但是要在该位置获取元素,只使用arrayName [index],。value将从数组中该位置的对象获取value属性 - 对于字符串,数字等,等不存在。例如,“myString”.value也将是未定义的。

#2


5  

JavaScript is a dynamically typed language. This means that you never need to declare the type of a function argument (or any other variable). So, your code will work as long as arrayP is an array and contains elements with a value property.

JavaScript是一种动态类型语言。这意味着您永远不需要声明函数参数(或任何其他变量)的类型。因此,只要arrayP是一个数组并且包含具有value属性的元素,您的代码就会起作用。

#3


2  

It is possible to pass arrays to functions, and there are no special requirements for dealing with them. Are you sure that the array you are passing to to your function actually has an element at [0]?

可以将数组传递给函数,处理它们没有特殊要求。你确定要传递给函数的数组实际上有一个元素在[0]吗?