访问数组列表中的对象

Access objects in array list

我正在尝试使用 javascript 访问数组列表中的对象,数组列表是:

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);
    }

这只是 returns 我

[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 脚本的新手,所以如果这是一个愚蠢的问题,请原谅我。

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

举个例子:

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

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

您可以使用 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);

要访问主要电子邮件,请尝试使用 membersList["users"]["primaryEmail"]

在 google 应用程序脚本中唯一对我有用的答案是:

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。非常感谢您的帮助。