如何在 TypeScript 中将 foreach 中的项添加到对象数组中?

huangapple go评论63阅读模式
英文:

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')

我试图:

  1. 将每个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

  1. 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>



huangapple
  • 本文由 发表于 2023年5月22日 01:18:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定