英文:
How to add an item from foreach to object array in typescript?
问题
在我的代码中,我有以下代码片段:
const accountEnv = 'Prod'
const accPermissionMap = {}
Object.values(Accounts).forEach(account => { const adminRole = ImportableRole.fromRoleArn(this, `${account.account}-Role`, 'arn:aws:iam::' + account.account + ':role/Admin')})
因此,在循环内部,当循环运行时,它将将以下值分配给“account”变量:
ImportableRole.fromRoleArn(this, `Prod-Role`, 'arn:aws:iam::' + Prod + ':role/Admin')
ImportableRole.fromRoleArn(this, `UAT-Role`, 'arn:aws:iam::' + UAT + ':role/Admin')
ImportableRole.fromRoleArn(this, `MT-Role`, 'arn:aws:iam::' + MT + ':role/Admin')
ImportableRole.fromRoleArn(this, `Dev-Role`, 'arn:aws:iam::' + Dev + ':role/Admin')
我试图:
- 将每个ImportableRole作为带有键的对象添加到“accPermissionMap”中。
因此,在添加之后,“accPermissionMap”应该类似于以下内容:
{
Key: Prod, Value: ImportableRole.fromRoleArn(this, `Prod-Role`, 'arn:aws:iam::' + Prod + ':role/Admin')
Key: UAT, Value: ImportableRole.fromRoleArn(this, `UAT-Role`, 'arn:aws:iam::' + UAT + ':role/Admin')
Key: MT, Value: ImportableRole.fromRoleArn(this, `MT-Role`, 'arn:aws:iam::' + MT + ':role/Admin')
Key: Dev, Value: ImportableRole.fromRoleArn(this, `Dev-Role`, 'arn:aws:iam::' + Dev + ':role/Admin')
}
有人能否提供一个解决方法?
英文:
In my Code I have below code snippet
const accountEnv = 'Prod'
const accPermissionMap = {}
Object.values(Accounts).forEach(account => { const adminRole = ImportableRole.fromRoleArn(this, `${account.account}-Role`, 'arn:aws:iam::' + account.account + ':role/Admin')})
As a result, Inside the foreach loop, it will assign below values to the "account" variable when the loop is running.
ImportableRole.fromRoleArn(this, `Prod-Role`, 'arn:aws:iam::' + Prod + ':role/Admin')
ImportableRole.fromRoleArn(this, `UAT-Role`, 'arn:aws:iam::' + UAT + ':role/Admin')
ImportableRole.fromRoleArn(this, `MT-Role`, 'arn:aws:iam::' + MT + ':role/Admin')
ImportableRole.fromRoleArn(this, `Dev-Role`, 'arn:aws:iam::' + Dev + ':role/Admin')
I'm trying to
- Add every ImportableRole to "accPermissionMap" as an object with a key.
So, after adding, "accPermissionMap" should be something similar to,
{
Key: Prod, Value: ImportableRole.fromRoleArn(this, `Prod-Role`, 'arn:aws:iam::' + Prod + ':role/Admin')
Key: UAT, Value: ImportableRole.fromRoleArn(this, `UAT-Role`, 'arn:aws:iam::' + UAT + ':role/Admin')
Key: MT, Value: ImportableRole.fromRoleArn(this, `MT-Role`, 'arn:aws:iam::' + MT + ':role/Admin')
Key: Dev, Value: ImportableRole.fromRoleArn(this, `Dev-Role`, 'arn:aws:iam::' + Dev + ':role/Admin')
}
Could someone please suggest a workaround?
答案1
得分: 3
你可以使用account.account
作为键,将每个ImportableRole
对象添加到accPermissionMap
中。然后,遍历accPermissionMap
的键,并检查是否有任何键与accountEnv
的值匹配。如果找到匹配项,你可以执行相应的操作,使用相应的ImportableRole
。此外,请确保你具有必要的导入语句,并且Accounts
对象已正确定义。
const accountEnv = 'Prod';
const accPermissionMap = {};
Object.values(Accounts).forEach(account => {
const adminRole = ImportableRole.fromRoleArn(this, `${account.account}-Role`, 'arn:aws:iam::' + account.account + ':role/Admin');
accPermissionMap[account.account] = adminRole; // 将每个 ImportableRole 添加到 accPermissionMap
});
// 检查哪个 ImportableRole 项目等于 accountEnv
Object.keys(accPermissionMap).forEach(account => {
if (account === accountEnv) {
// 如果匹配,执行所需的操作
const matchedRole = accPermissionMap[account];
// 使用匹配的角色执行某些操作
console.log(`Found a match for accountEnv (${accountEnv}) with role:`, matchedRole);
// 使用匹配的角色执行所需的操作
}
});
英文:
You can add each ImportableRole
object to the accPermissionMap
using the account.account
as the key. Then, iterate over the keys of accPermissionMap
and check if any of them match the accountEnv
value. If a match is found, you can perform your desired action with the corresponding ImportableRole
. Also, make sure you have the necessary import statements and that the Accounts
object is defined correctly.
const accountEnv = 'Prod';
const accPermissionMap = {};
Object.values(Accounts).forEach(account => {
const adminRole = ImportableRole.fromRoleArn(this, `${account.account}-Role`, 'arn:aws:iam::' + account.account + ':role/Admin');
accPermissionMap[account.account] = adminRole; // Add each ImportableRole to accPermissionMap
});
// Check which ImportableRole item is equal to accountEnv
Object.keys(accPermissionMap).forEach(account => {
if (account === accountEnv) {
// If it matches, perform the desired action
const matchedRole = accPermissionMap[account];
// Do something with the matched role
console.log(`Found a match for accountEnv (${accountEnv}) with role:`, matchedRole);
// Perform your desired action with the matched role
}
});
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论