简体   繁体   English

传递给函数后数组丢失

[英]Array is lost after passed to a function

I'm getting a length of 0 for the incoming array to this method below and undefined for the array properties that's being passed in below and not sure why: 我在下面的此方法的传入数组的长度为0,在下面传递的数组属性的长度为undefined,不确定为什么:

function BindAlbumDropdownList(aAlbums, defaultAlbumID)
{
    if(aAlbums.length == 0)
        return;

    var albumDropdownID = $("#abumDropdown").attr("id");

    AddDropdownItem("-Select an Album-", "-1", "albumDropdown");

    for(var a in aAlbums)
    {
        alert("a.name, a.id, albumDropdown: " + a.name + "|" + a.id + "|" +  albumDropdownID);
        AddDropdownItem(a.name, a.id, albumDropdownID);
    }
}

I'm passing in an array that was defined earlier in another method that calls this method and the array was defined and passed like this: 我传入的是先前在另一个方法中定义的数组,该方法调用此方法,并且该数组的定义和传递方式如下:

var aAlbums = GetAllAlbums(userID, accessToken);
var defaultAlbumID = aAlbums[0].id;

BindAlbumDropdownList(aAlbums, defaultAlbumID)

GetAllAlbums is returning a variable setup like this inside it: GetAllAlbums返回的变量设置如下:

var aFacebookAlbums = []; // array

and returns this array once populated. 并在填充后返回此数组。

but when I enter the for loop in the BindAlbumDropdownList, I'm getting undefined for the array properties as well as the check for length is zero 但是当我在BindAlbumDropdownList中输入for循环时,我对数组属性以及长度检查是否为零都不确定

here's what the alert is giving me: 这是警报给我的:

a.name, a.id, albumDropdown: undefined|undefined|albumDropdown

I am positive GetAllAlbums is returning an array with values in it because 我很肯定GetAllAlbums返回一个包含值的数组,因为
var defaultAlbumID = aAlbum[0].id; gives me a valid value 给我一个有效的值

I noticed that it seems like BindAlbumDropdownList doesn't really know the incoming aAlbums is an array even though I am passing an array to it. 我注意到,即使我将数组传递给BindAlbumDropdownList,它似乎也不真正知道传入的aAlbums是一个数组。 Because I'm getting no intellisense showing the property array when trying to do aAlbum.length but I do get the .length property on the array just before I return it from the GetAllAlbums function. 因为在尝试执行aAlbum.length时没有智能感知显示属性数组,但是在从GetAllAlbums函数返回它之前,我确实获得了数组的.length属性。

you should use for statement and not for-in statement 您应该使用for语句而不是in-in语句

for(i=0; i < aAlbums.length; i++)
{
    var a = aAlbums[i];
    alert("a.name, a.id, albumDropdown: " + a.name + "|" + a.id + "|" +  albumDropdownID);
    AddDropdownItem(a.name, a.id, albumDropdownID);
}

Are you sure you included jQuery ok? 您确定您包含jQuery可以吗? Is "abumDropdown" correct for the selector id? 选择器ID是否正确“ abumDropdown”? You can use for...in with an array in javascript even though it's not recommended. 即使不建议使用,也可以将...用于javascript中的数组。

crap Ive never gotten one before and in my excitement I answered in a comment lol!! 我从来没有得到过胡扯,我兴奋地在评论中回答了!

what would you get if you asked for the value of 如果您要求的价值,您会得到什么?

0.name 1.name 2.name 0.名称1.名称2.名称

?

now what would you get if you asked for the value of 现在,如果您要求的价值是什么

a[0].name a[1].name a[2].name a [0]。名称a [1]。名称a [2]。名称

 for(var a in aAlbums)
    {
        alert("aAlbums[a].name, aAlbums[a].id, albumDropdown: " + a.name + "|" + a.id + "|" +  albumDropdownID);
        AddDropdownItem(aAlbums[a].name, aAlbums[a].id, albumDropdownID);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM