gpt4 book ai didi

javascript - 如何访问http响应中的嵌套数组和对象

转载 作者:行者123 更新时间:2023-12-02 21:44:25 24 4
gpt4 key购买 nike

我有一个包含对象和数组的嵌套数据结构。如何提取信息,即访问特定或多个值(或键)?

{
"data": [
{
"id": 2,
"addEmployee": {
"firstName": "Devansh"
},
"attendances": [
{
"id": 3,
"checkIn": "2020-02-18T04:36:45.000Z",
"checkOut": "2020-02-18T11:46:16.000Z",
"totalHours": "7:9",
"date": "2020-02-18",
"status": "present",
"createdAt": "2020-02-18T04:36:45.000Z",
"updatedAt": "2020-02-18T11:46:16.000Z",
"userId": 2
}
]
}
]
}

我如何访问名字和出勤数组值

最佳答案

您必须将对象分配给变量,就像我在这里分配给 val 变量一样。您可以按如下方式访问属性。您可以在此处运行代码片段并查看输出

let val =  {
"data": [
{
"id": 2,
"addEmployee": {
"firstName": "Devansh"
},
"attendances": [
{
"id": 3,
"checkIn": "2020-02-18T04:36:45.000Z",
"checkOut": "2020-02-18T11:46:16.000Z",
"totalHours": "7:9",
"date": "2020-02-18",
"status": "present",
"createdAt": "2020-02-18T04:36:45.000Z",
"updatedAt": "2020-02-18T11:46:16.000Z",
"userId": 2
}
]
}
]
}

val.data.forEach(eachData => {
console.log('Employee Name ---> ',eachData.addEmployee.firstName);
eachData.attendances.forEach(atten => {
console.log('attendance Object -->',atten);
})
});

关于javascript - 如何访问http响应中的嵌套数组和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60293818/

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