JavaScript中解析JSON响应并访问数组

3
我从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(有时候seat_urls可能为空)

任何帮助将不胜感激。

谢谢


你缺少了响应。使用 emp.response 就可以解决这个问题。 - sibabrat swain
@sibabratswain 我试着添加 ".response",但问题仍然存在。 - AbdA
请检查此链接 https://codepen.io/sibabrat_swain/pen/RwrWMeJ 并查看您的控制台。 - sibabrat swain
7个回答

3
请查看下方带有运行代码的更新后的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);


返回所有与座位链接匹配的emp.response,这只返回匹配的座位链接。 - AbdA

0

我认为

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

是错误的。应该是

emp.Response[0].seating..

emp.Response[1].seating..


0
从外观上看,emp 是一个包含数组的对象。你需要做类似于...的事情。
emp.Response[0].seating.....

或者可以将响应映射

emp.Response.map(myEmp=>{emp.Response[0].seating....})

0

这个答案需要解释。 - sidgate

0

解决方案:

您正在直接比较emp.seating.seat_urls,而它们是数组,您必须对数组中的每个项目执行它!尝试下面的代码!

将seat_curls存储在变量中以简化代码,如果有效,则可以在之后替换它!

并将整个过程放入函数中,以便更好地控制工作!

let shiftA = ["/api/1/seats/50003","/api/1/seats/62229", "/api/1/seats/556565"];
let seatUrls = emp.seating.seat_urls;

function compareSeating(shift1, shiftResponse){
  let shiftAEmp = [];
  shift1.forEach((itemsOfShiftA) => shiftResponse.forEach(itemsOfShiftResponse) => {
     if (itemsOfShiftA === itemsOfShiftResponse) {
        shiftAEmp.push(itemsOfShiftA)
     }
  }
));
return shiftAEmp;
}

0
var shiftAEmp =  emp.response[0].seating.seat_urls.filter(value => shiftA.includes(value))
//for the first element since its an array
//OR
//for all
var shiftAEmp = emp.response.map(a=>{
  return a.seating.seat_urls.filter(value => shiftA.includes(value))
});

0

我会用每个循环向您展示解决方案。请跟随这个。

resultantArray = [];
emp.response.forEach((arr) => {
    if(arr.online_seating.seat_url) {
      resultantArray.push(...arr.online_seating.seat_url)
    }
    if(arr.seating.seat_urls) {
      resultantArray.push(...arr.seating.seat_urls)
    }
});


const intersection = resultantArray.filter(x => shiftA.includes(x));
console.log(intersection);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接