访问数组列表中的对象

时间:2018-06-12 19:59:37

标签: javascript google-apps-script gsuite

我正在尝试使用javascript访问对象arraylist,arraylist是:

membersList:{
    "kind":"admin#directory#users",
    "users":[{"orgUnitPath":"/",
        "isMailboxSetup":true,
        "id":"1076823423424234",
        "isAdmin":false,
        "suspended":false,
        "isDelegatedAdmin":false,
        "isEnforcedIn2Sv":false,
        "etag":"\"npJcgsdfsadfsfsff\"",
        "ipWhitelisted":false,"changePasswordAtNextLogin":true,
        "customerId":"C01looera",
        "includeInGlobalAddressList":true,
        "lastLoginTime":"1970-01-01T00:00:00.000Z",
        "primaryEmail":"ilda.donofrio@domain.org",
        "isEnrolledIn2Sv":false,"kind":"admin#directory#user",
        "name":{"givenName":"ilda",
            "familyName":"donofrio",
            "fullName":"ilda donofrio"},
            "creationTime":"2018-06-10T11:56:45.000Z",
            "emails":[{"address":"ilda.donofrio@domain.org",
            "primary":true}],
            "agreedToTerms":true
            }],
        "etag":"\"npJcgsdfsadfsfsff/npJcgsdfsadfsfsff\"

    }

我正试图从我为其编写代码的列表中访问primary email,即

for (var j in membersList) {
       var member = membersList[j];
       Logger.log('member Email:' + member);
    }

只是让我回来

[18-06-12 15:39:55:982 EDT] member Email:admin#directory#users

[18-06-12 15:39:55:982 EDT] member Email:{"orgUnitPath":"/","isMailboxSetup":true,"id":"1076823423424234","isAdmin":false,"suspended":false,"isDelegatedAdmin":false,"isEnforcedIn2Sv":false,"etag":"\"npJcgsdfsadfsfsff\"","ipWhitelisted":false,"changePasswordAtNextLogin":false,"customerId":"C01looera","includeInGlobalAddressList":true,"lastLoginTime":"1970-01-01T00:00:00.000Z","primaryEmail":"ilda.donofrio@domain.org","isEnrolledIn2Sv":false,"kind":"admin#directory#user","name":{"givenName":"ilda","familyName":"donofrio","fullName":"ilda donofrio"},"creationTime":"2018-06-10T11:56:45.000Z","emails":[{"address":"ilda.donofrio@domain.org","primary":true}],"agreedToTerms":true}

[18-06-12 15:39:55:983 EDT] member Email:"npJcgsdfsadfsfsff/npJcgsdfsadfsfsff\"

当我尝试使用时:

Logger.log('member Email:' + member.primaryEmail);

它的投掷:

[18-06-12 16:02:53:630 EDT] member Email:undefined
[18-06-12 16:02:53:630 EDT] member Email:undefined
[18-06-12 16:02:53:631 EDT] member Email:undefined

有没有办法使用脚本提取primaryEmail的值?我是javascript和google脚本的新手所以请原谅我,如果它是一个愚蠢的问题。

4 个答案:

答案 0 :(得分:1)

您应该遍历成员列表对象中的所有users,然后检索每个用户primaryEmail。目前,您正在迭代membersList对象的所有属性(即键,其中一个是users)。

举个例子:

membersList['users'].forEach((user) => {
  Logger.log('member Email: ' + user['primaryEmail']);
}

迭代的方式取决于你,我只是倾向于在JS中使用.forEach

答案 1 :(得分:0)

您可以使用membersList.users[0].primaryEmail访问主电子邮件。

此外,在最后一位("etag":"\"npJcgsdfsadfsfsff/npJcgsdfsadfsfsff\")上,最后一个\退出字符串,但不应该,但请尝试将其删除。

var membersList = {
    "kind":"admin#directory#users",
    "users":[{"orgUnitPath":"/",
        "isMailboxSetup":true,
        "id":"1076823423424234",
        "isAdmin":false,
        "suspended":false,
        "isDelegatedAdmin":false,
        "isEnforcedIn2Sv":false,
        "etag":"\"npJcgsdfsadfsfsff\"",
        "ipWhitelisted":false,"changePasswordAtNextLogin":true,
        "customerId":"C01looera",
        "includeInGlobalAddressList":true,
        "lastLoginTime":"1970-01-01T00:00:00.000Z",
        "primaryEmail":"ilda.donofrio@domain.org",
        "isEnrolledIn2Sv":false,"kind":"admin#directory#user",
        "name":{"givenName":"ilda",
            "familyName":"donofrio",
            "fullName":"ilda donofrio"},
            "creationTime":"2018-06-10T11:56:45.000Z",
            "emails":[{"address":"ilda.donofrio@domain.org",
            "primary":true}],
            "agreedToTerms":true
            }],
        "etag":"\"npJcgsdfsadfsfsff/npJcgsdfsadfsfsff"

    }
    
console.log(membersList.users[0].primaryEmail);

答案 2 :(得分:0)

要访问primaryEmail,请尝试使用membersList["users"]["primaryEmail"]

答案 3 :(得分:0)

谷歌应用脚​​本中唯一有效的答案是:

for (var j in membersList) {
       var member = membersList[j];
       for (var k in member){        
         var user =  member[k];
         var memberEmail = user.primaryEmail;
         var lastLoginTime = user.lastLoginTime;
       } 
    }
    Logger.log('member Email:' + memberEmail);
    Logger.log('last Login Time:' + lastLoginTime);

我尝试了在这里发布的每个方法,但是没有成功,但正如@AllenG引用的那样,我只需要遍历成员列表对象中的所有users。非常感谢你的帮助。