传递一个数组在JavaScript参数 [英] Passing an array as parameter in JavaScript

查看:157
本文介绍了传递一个数组在JavaScript参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我希望把它作为一个函数,如参数:

 函数的东西(arrayP){
    对于(VAR I = 0; I< arrayP.length;我++){
          警报(arrayP [I]。价值);
    }
 }

我收到arrayP [0]是不确定的,因为里面的功能我从来没有写过arrayP是什么类型的数组这可能是真实的。因此,


  1. 是可以传递数组作为参数?

  2. 如果是这样,这是函数内部的要求是什么?


解决方案

只需删除 .value的,就像这样:

 功能(arrayP){
   对于(VAR I = 0; I< arrayP.length;我++){
      警报(arrayP [I]); //这里没有。价值
   }
}

当然,你可以传递一个阵列,但在该位置获得元素,使用的只有 arrayName中的[索引] .value的将获得属性关闭对象的数组中的位置 - 这对于像字符串,数字等不存在。例如,的myString.value的也将是未定义

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,

  1. Is is possible to pass arrays as parameters?
  2. If so, which are the requirements inside the function?

解决方案

Just remove the .value, like this:

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.

这篇关于传递一个数组在JavaScript参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆