Passing an array as parameter in JavaScript

后端 未结 3 1202
既然无缘
既然无缘 2020-12-28 15:06

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++){
              


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-28 15:35

    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.

提交回复
热议问题