Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an Array inside A Parsed JSON Response in Java Script

I have the following parsed JSON response from an API

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",
    }
  ]
}

I need to compare the array seat_urls that is inside that response to the following array

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

And return all the data from emp if any of the URLs in shiftA matches the URLs in emp.seat_urls

I have tried

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

and many others but I keep getting TypeError: Cannot read property 'seat_urls' of undefined

I tried other filter approaches with no luck it just seam I can't access the seat_urls inside the JSON file emp (sometimes seats_urls cab=n be empty by the way)

Any help will be appreciated

Thanks

like image 562
AbdA Avatar asked Sep 14 '26 21:09

AbdA


1 Answers

Please check the updated emp object below with running code:

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);
like image 85
solanki... Avatar answered Sep 16 '26 14:09

solanki...