Reputation: 1010
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
Upvotes: 3
Views: 93
Reputation: 534
var shiftAEmp = emp.response.filter(r => {
return r.seating.seat_urls.some(u => shiftA.includes(u));
)
For some
method, refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
The
some()
method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
Upvotes: 0
Reputation: 5098
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);
Upvotes: 3
Reputation: 1368
I will show you the solution with each loop. Please follow this.
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);
Upvotes: 0
Reputation: 66
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))
});
Upvotes: 0
Reputation: 301
SOLUTION:
You are comparing the emp.seating.seat_urls
directly, whereas they are arrays, you have to execute it with every item in the array!
Try below code!
Get the seat_curls in a variable to simplify the code, and if it works then replace it after!
And bring the whole thing in a function to have proper control over the work!
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;
}
Upvotes: 0
Reputation: 1236
I think
var shiftAEmp = emp.seating.seat_urls.filter(value => shiftA.includes(value))
is wrong. It should be
emp.Response[0].seating..
emp.Response[1].seating..
Upvotes: 0
Reputation: 19
From the looks of things emp
is an object that contains an array. You'd need to do something like...
emp.Response[0].seating.....
Or alternatively map the responses
emp.Response.map(myEmp=>{emp.Response[0].seating....})
Upvotes: 0