javascript - 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),
提前致谢!
解决方案
我重写了代码,以便更好地理解它。据我所知,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')
}
推荐阅读
- asp.net-mvc - 自定义异常 ASP.NET MVC
- sharepoint-2013 - 将文档(pdf、xls)从一个库移动到另一个库时如何保持适当的文件类型 (MIME)
- angular-material - 填充高度的网格列表的内容
- mysql - 优化 SUM() AND GROUP BY MySQL 查询
- function - CQL using a set/list in a CQL query with IN clause
- python - 在mathematica中构建的数据的pyomo中的反序列化
- nginx - 如何在 nginx 上安装模块?
- google-chrome-extension - 是否可以在 Chrome 扩展程序中设置 BadgeText 的字体大小?
- javascript - chrome 开发工具中的无头 HTML5 性能捕获
- c# - 实体框架:一对多?