javascript - 打字稿中的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';
}
}
解决方案
要访问每个角色组项中的数据,您只需在 for 循环中从以下位置开始:items.end 或 items.parentGroupId。每次迭代的项目都有它所接触的每个元素的数据,这是由 items.[attribute] 访问的。
推荐阅读
- python - 单独列出目录中的文件python
- react-native - 如何使用 Zendesk Upload API 上传图片
- python - 使用 Python Selenium 查找元素
- xml - 标签后如何获取和转换属性
输出日期:
<products> <group> <product id="73" symbol="LEC-PY1010C" ean="5901436709251" price_pln="29.05" price_eur="6.56" tax="23.0000" stock="1215"> <
- javascript - 如何在 v-for 循环中更改数组对象中的状态?
- python - 在不同的行上循环打印
- javascript - 需要访问订阅功能里面的元素淘汰js
- python - 如何通过从另一行获取来填充数据框中的 Na 值
- html - 为什么它进入一个新的生产线而不是继续统一?
- android - 如何制作一个实心圆形进度条?(不使用外部库)