javascript - 如何从 api 响应中迭代复杂对象
问题描述
这是一个 api 响应,我想从响应中迭代所有appId
和userInfo
。我如何迭代这个响应?
{
"status_code": "SUCCESS",
"status": "SUCCESS",
"message": "Success",
"data": [
{
"_id": "6034acaaf751f1c89721a76b",
"status": "active",
"name": "testname",
"collectionStatus": "completed",
"createdDate": 1614064810201,
"subscriberStats": [],
"platforms": [
{
"_id": "6034acaa47a83e45b3f5fc4a",
"status": "active",
"traffic": 0,
"parentStatus": "active",
"platformType": "website",
"appId": "xxx",
"optinShowInterval": 0,
"optInFrequencyType": "page_refresh",
"webType": "wordpress",
"optInFrequency": 0,
"userId": "xxx",
"createdDate": 1614064810563
}
]
},
{
"_id": "600ae310255964c2e21a580f",
"status": "active",
"name": "trial",
"collectionStatus": "completed",
"createdDate": 1611326224761,
"subscriberStats": [],
"platforms": [
{
"_id": "600ae31163d6d321f4693d1d",
"status": "active",
"traffic": 0,
"parentStatus": "active",
"platformType": "website",
"webType": "wordpress",
"appId": "xxx",
"optinShowInterval": 0,
"optInFrequencyType": "page_refresh",
"optInFrequency": 0,
"userId": "xxx",
"createdDate": 1611326225013
}
]
}
]
}
结果输出应该是一个对象数组,例如:
[
{appId: "", userInfo: ""},
{appId: "", userInfo: ""}
]
解决方案
尝试添加一些循环:
const result = [];
for (const a in response) {
for (const b of response[a]) {
result.push({
appId: b._id,
userInfo: b.parentStatus // or some other properties
})
}
}
console.log(result)
推荐阅读
- python - 根据用户输入生成唯一数量的问题并将输入收集到列表中
- php - 如何在php的网页中嵌入word文档
- hive - Hive:何时运行 tez 任务,何时不运行
- lirc - LIRC 驱动程序选项 - 默认 vs devinput
- android - PyTorch 参数在 android 上无效
- java - 我如何让 paincomponent 从其他方法或类中提取东西
- java - 如果任何线程发生异常,则中断所有线程
- c - c语言中的凯撒分段错误
- android - 单击标记时打开不同的活动(多个标记)
- azure-ad-b2c - 如何在 AAD B2C 中添加 Slack 作为身份提供者?