gpt4 book ai didi

javascript - 我想展平嵌套的对象数组并将它们转换为一个对象数组

转载 作者:行者123 更新时间:2023-12-03 07:23:42 25 4
gpt4 key购买 nike

我有来自后端的以下类型的 JSON 响应。我在网格表中显示此响应。为此,我只需要整个响应中的“项目”对象数组。我无法将所有项目整合在一起。

[{
"responseText": "Success",
"userName": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4d5e51555a5a4b114c570e0b7f58525e5653115c5052" rel="noreferrer noopener nofollow">[email protected]</a>",
"userId": 2,
"projectDetails": [{
"accountName": "ViewEnvironment",
"projects": [{
"responseText": "success",
"id": 34,
"projectName": "Plato",
"accountName": "ViewEnvironment",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "DL/PM",
"roleId": 3,
"projectUserId": 89
}]
},
{
"accountName": "Accloud",
"projects": [{
"responseText": "success",
"id": 4,
"projectName": "Citi Test2",
"accountName": "Accloud",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Executive",
"roleId": 2,
"projectUserId": 83
},
{
"responseText": null,
"id": 5,
"projectName": "Citi Test3",
"accountName": "Accloud",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Executive",
"roleId": 2,
"projectUserId": 221
},
{
"responseText": null,
"id": 9,
"projectName": "Test_Project",
"accountName": "Accloud",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Executive",
"roleId": 2,
"projectUserId": 220
}
]
},
{
"accountName": "iBASE",
"projects": [{
"responseText": "success",
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Admin",
"roleId": 1,
"projectUserId": 70
},
{
"responseText": null,
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Admin",
"roleId": 1,
"projectUserId": 72
},
{
"responseText": null,
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Admin",
"roleId": 1,
"projectUserId": 73
},
{
"responseText": null,
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Developer",
"roleId": 5,
"projectUserId": 74
}
]
}
]
}]

我尝试使用嵌套 for 循环来执行此操作,但只有最后一个数组出现,而不是所有数组。就像下面这样。

for (let user of this.selectedUser) {
this.projectList = user.projectDetails;
for (project of this.projectList) {
(this.projectList2) = project.projects;

for (let proj of this.projectList2) {
this.finalProjectsList = proj;
}
}
}
console.log("projcetlist", this.projectList.projects)

任何人都可以建议更好的方法来获得正确的响应。

最佳答案

如果您使用的浏览器不支持Array.flatMap ,您可以使用嵌套 Array.reduce 来实现您想要的结果:

const projectList = selectedUser.reduce((p, {  projectDetails }) =>
p.concat(projectDetails.reduce((c, { projects }) =>
c.concat(projects), [])
),
[]);

const selectedUser = [{
"responseText": "Success",
"userName": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a786b64606f6f7e2479623b3e4a6d676b636624696567" rel="noreferrer noopener nofollow">[email protected]</a>",
"userId": 2,
"projectDetails": [{
"accountName": "ViewEnvironment",
"projects": [{
"responseText": "success",
"id": 34,
"projectName": "Plato",
"accountName": "ViewEnvironment",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "DL/PM",
"roleId": 3,
"projectUserId": 89
}]
},
{
"accountName": "Accloud",
"projects": [{
"responseText": "success",
"id": 4,
"projectName": "Citi Test2",
"accountName": "Accloud",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Executive",
"roleId": 2,
"projectUserId": 83
},
{
"responseText": null,
"id": 5,
"projectName": "Citi Test3",
"accountName": "Accloud",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Executive",
"roleId": 2,
"projectUserId": 221
},
{
"responseText": null,
"id": 9,
"projectName": "Test_Project",
"accountName": "Accloud",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Executive",
"roleId": 2,
"projectUserId": 220
}
]
},
{
"accountName": "iBASE",
"projects": [{
"responseText": "success",
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Admin",
"roleId": 1,
"projectUserId": 70
},
{
"responseText": null,
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Admin",
"roleId": 1,
"projectUserId": 72
},
{
"responseText": null,
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Admin",
"roleId": 1,
"projectUserId": 73
},
{
"responseText": null,
"id": 1,
"projectName": "iBase-Project-edit",
"accountName": "iBASE",
"projectHealth": null,
"modulesCount": 0,
"cordinatorEmail": null,
"businessType": null,
"projectType": null,
"status": null,
"createdDate": null,
"createdBy": null,
"modifiedDate": null,
"modifiedBy": null,
"role": "Developer",
"roleId": 5,
"projectUserId": 74
}
]
}
]
}];

const projectList = selectedUser.reduce((p, { projectDetails }) =>
p.concat(projectDetails.reduce((c, { projects }) =>
c.concat(projects), [])), []);
console.log(projectList)

关于javascript - 我想展平嵌套的对象数组并将它们转换为一个对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64735776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com