首页 > 解决方案 > javascript 从地图中检索值

问题描述

我正在尝试开发一个谷歌脚本应用程序。

这是创建数组映射的一个函数。

function getOffices(){
 var result=AdminDirectory.Groups.list({domain:"example.com"})
  result=result.groups.filter(function(group){
    var str=group.email;
     return str.search("-office@example.com")>=0;
  })
  result=result.map(function(group){ return {name:group.name,email:group.email}})
 
  return result;
}

我创建了一个逻辑块,我想根据结果执行某些操作,如下所示:

var getOrgUnitPath = (accountOffice, accountType) => {
if (accountType === 'facilitator') {
  return 'Limited Accounts/Gmail Plus Calendar';
} else {
  switch (accountOffice) {
    case accountOffice.includes('Boston'):
      return "/Standard-Access/Boston";
      break;
    case accountOffice.includes('New York'):
      return '/Standard-Access/New York';
      break;
    case accountOffice.includes('Lincoln'):
      return '/Standard-Access/Lincoln';
      break;
    default:
      return '/Standard-Access';
      break;
  }
}

};

最后,我尝试设置组织单位——这最终是我想要做的,但似乎无法正确使用语法,我已经尝试了我能想到的一切。我已经硬编码了“accountType”并且它工作了,所以我知道 formObject.accountType 运行正常。

orgUnitPath: getOrgUnitPath(accountType, formObject.accountType),

提前致谢!

标签: javascriptdictionarygoogle-apps-script

解决方案


我重写了代码,以便更好地理解它。据我所知,getOffices列出所有 office 并getOrgUnitPath返回一个路径,包括与 office 的有序列表匹配的第一个 office ['Boston', 'NY', 'Lincoln']。如果是这样,那么缺少的是第一个参数getOrgUnitPath应该是getOffices(),对吗?(注意这是函数的执行getOffices。)

这是我喜欢的“简化”代码。我希望它有帮助:

const getOffices = () => {
  const bigList = x.y.list({ domain: 'example.com' }) // ?
  return bigList
    .filter(cur => ~cur.email.search('abc'))
    .map(cur => ({
      name: cur.name,
      email: cur.email
    }))
}

const getPath = (accOffice, accType) => {
  if (accType === 'xyz')
    return 'foobar'
  const city = ['Boston', 'NY', 'Lincoln']
    .find(cur => accOffice.includes(cur))
  return `yadayada/${city}`
}

const theFinalObj = {
  orgUnitPath: getPath(getOffices(), 'rightHardcodedType')
}

推荐阅读