首页 > 解决方案 > 打字稿中的for循环用于提取dynamodb数据

问题描述

我是打字稿的新手。我正在尝试使用打字稿从 dynamodb 中提取数据。我正在尝试从一个可以有 n 个列表并且每个列表都有映射器的字段中提取数据。我需要编写一个 for 循环,以便它可以通过 n 个列表来提取 n 个列表的每个映射器的数据。

dynamodb 中带有 1 个列表的示例数据:

  "roleGroups": [
    {
      "end": "2021-12-29#PM",
      "id": "3830122-6c5-df63-6abf-1f3b5a2e1",
      "name": "iOS",
      "start": "2021-08-30#AM"
    }

dynamodb 中包含 5 个列表的示例数据:

 "roleGroups": [
    {
      "end": "2021-12-31#PM",
      "id": "0e08a23-e3cc-4ad6-1448-837d6a0af7a",
      "name": "Product operating model",
      "start": "2021-09-01#AM"
    },
    {
      "end": "2021-12-31#PM",
      "id": "a05f2a-e58f-6172-487-064d7c8be352",
      "name": "Loyalty programme",
      "start": "2021-09-01#AM"
    },
    {
      "end": "2022-01-07#PM",
      "id": "20186db-02e-31c5-04dc-73731585a",
      "name": "Click and Collect",
      "start": "2021-09-01#AM"
    },
    {
      "end": "2022-01-06#PM",
      "id": "70fb84-a7e3-32d3-ee7-3502810e8",
      "name": "Mobile",
      "parentGroupId": "20186db-02e-31c5-04dc-73731585a",
      "start": "2021-09-01#AM"
    },
    {
      "end": "2022-01-07#PM",
      "id": "d3bef7-1dc1-bbcf-50ec-c86e1d35357b",
      "name": "Web",
      "parentGroupId": "20186db-02e-31c5-04dc-73731585a",
      "start": "2021-09-01#AM"
    }
  ],

这是我到目前为止的代码:

 if (
    newImage.roleGroups != null &&
    newImage.roleGroups.L != null &&
    newImage.roleGroups.L.length > 0 &&
    newImage.roleGroups.L[0].M != null
  ) {
    
    //for loop through the list of rolegroups for ()

  for (const items in roleGroups) {

    const recordsToJoinEngagementsGroups: string[] = [
      record.eventName,
      record.eventSourceARN.split('/')[1],
      '0',
      '1900/01/01 00:00:00',
    ];

    recordsToJoin.push(
      getAttributeValue(newImage.engagementId),
      getAttributeValue(newImage.id),
      getAttributeValue(newImage.parentGroupId),
      getAttributeValue(newImage.name),

      getAttributeValue(newImage.start),
      getAttributeValue(newImage.end),
      getAttributeValue(newImage.createdat, true),
      getAttributeValue(newImage.updatedat, true),
      );

      const latestUpdatedTime = Math.max(
        Number(newImage.updatedAt.N),
        Number(newImage.createdAt.N),
      );
      recordsToJoin.push(decodeTimestamp(latestUpdatedTime));

    dataStreamEngagementsGroups +=
      recordsToJoinEngagementsGroups
        .join(separator)
        .replace(/(?:\r\n|\r|\n)/g, ' ') + '\n';
  }

   }

标签: javascripttypescriptamazon-dynamodbamazon-dynamodb-streams

解决方案


要访问每个角色组项中的数据,您只需在 for 循环中从以下位置开始:items.end 或 items.parentGroupId。每次迭代的项目都有它所接触的每个元素的数据,这是由 items.[attribute] 访问的。


推荐阅读