使用自定义属性的迭代和迭代数组的长度

Mar*_*ark 2 javascript arrays

var profileDataCalls = [];

profileDataCalls['Profile'] = GetUserAttributesWithDataByGroup;
profileDataCalls['Address'] = GetUserAddresses;
profileDataCalls['Phone'] = GetUserPhoneNumbers;
profileDataCalls['Certs'] = GetUserCertifications;
profileDataCalls['Licenses'] = GetUserLicenses;
profileDataCalls['Notes'] = GetUserNotes;
Run Code Online (Sandbox Code Playgroud)

我的问题是上面的JavaScript数组只有0的长度.我需要一个可以迭代的数组并保存键(字符串)和值?

Dav*_*mas 6

你要:

var profileDataCalls = {
    'Profile' : GetUserAttributesWithDataByGroup,
    'Address' : GetUserAddresses,
    'Phone' : GetUserPhoneNumbers,
    'Certs' : GetUserCertifications,
    'Licenses' :GetUserLicenses,
    'Notes' : GetUserNotes
};
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用,例如,profileDataCalls.profile或访问值profileDataCalls[profile](以检索由变量表示的任何值GetUserAttributesWithDataByGroup)

要遍历对象,请使用:

for (var property in profileDataCalls) {
    if (profileDataCalls.hasOwnProperty(property)) {
        console.log(property + ': ' + profileDataCalls[property));
    }
}
Run Code Online (Sandbox Code Playgroud)

  • +1现在你终于用你的`for ... in`循环回答了*actual*问题. (2认同)