gpt4 book ai didi

javascript - 在 Java 脚本中访问已解析的 JSON 响应中的数组

转载 作者:行者123 更新时间:2023-12-01 16:10:09 25 4
gpt4 key购买 nike

我有以下来自 API 的已解析 JSON 响应

emp = 
{
"response": [
{
"image_fingerprint": null,
"image_source_fingerprint": null,
"last_name": "LastName",
"location": "G564",
"notes": "A great worker",
"online_seating": {
"seat_urls": []
},
"photo": "/images/employee.jpg",
"seating": {
"seated": "not seated",
"seat_urls": [
"/api/1/seats/33444",
"/api/1/seats/55323",
"/api/1/seats/62229"
]
},
"show_in_vd": true,
"start_date": "2014-01-02",
},

{
"image_fingerprint": null,
"image_source_fingerprint": null,
"last_name": "LastName",
"location": "G564",
"notes": "A great worker",
"online_seating": {
"seat_urls": []
},
"photo": "/images/employee.jpg",
"seating": {
"seated": "not seated",
"seat_urls": [
"/api/1/seats/56580",
"/api/1/seats/69856",
"/api/1/seats/50003"
]
},
"show_in_vd": true,
"start_date": "2014-01-02",
}
]
}

我需要将该响应中的数组 seat_urls 与以下数组进行比较

shiftA = ["/api/1/seats/50003","/api/1/seats/62229", "/api/1/seats/556565"]

如果 shiftA 中的任何 URL 与 emp.seat_urls 中的 URL 匹配,则返回来自 emp 的所有数据

我试过了

var shiftAEmp =  emp.seating.seat_urls.filter(value => shiftA.includes(value))

和许多其他人,但我不断收到 TypeError: Cannot read property 'seat_urls' of undefined

我尝试了其他过滤方法,但不幸的是它只是接缝我无法访问 JSON 文件 emp 中的 seat_urls (顺便说一句,有时 seats_urls cab=n 为空)

任何帮助将不胜感激

谢谢

最佳答案

请使用运行代码检查下面更新的 emp 对象:

var emp = {
"response": [{
"image_fingerprint": null,
"image_source_fingerprint": null,
"last_name": "LastName",
"location": "G564",
"notes": "A great worker",
"online_seating": {
"seat_urls": []
},
"photo": "/images/employee.jpg",
"seating": {
"seated": "not seated",
"seat_urls": [
"/api/1/seats/33444",
"/api/1/seats/55323",
"/api/1/seats/62229"
]
},
"show_in_vd": true,
"start_date": "2014-01-02",
},
{
"image_fingerprint": null,
"image_source_fingerprint": null,
"last_name": "LastName",
"location": "G564",
"notes": "A great worker",
"online_seating": {
"seat_urls": []
},
"photo": "/images/employee.jpg",
"seating": {
"seated": "not seated",
"seat_urls": [
"/api/1/seats/56580",
"/api/1/seats/69856",
"/api/1/seats/50003"
]
},
"show_in_vd": true,
"start_date": "2014-01-02",
}
]
};

var shiftA = ["/api/1/seats/50003","/api/1/seats/62229", "/api/1/seats/556565"];
var rd = emp.response.filter(r => r.seating.seat_urls.some(u => shiftA.includes(u)));

console.log(rd);

关于javascript - 在 Java 脚本中访问已解析的 JSON 响应中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62274473/

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